PHP Date - How to add a string to separate date and time
Asked Answered
D

6

6

I want to display date and time format something like this "May 23 at 12:30pm". I saw in PHP manual and found:

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

After modification I manage to get

 echo date('M j \of h:i a');

it is giving me "May 23 of 12:30pm"

but when i replacing of with at it is giving me "May 23 a23 08:26 pm".

I don't what is going wrong.

Doody answered 31/5, 2013 at 16:9 Comment(0)
A
17

you need to escape the a and t as both have special meaning when used as formatting options in date()

echo date('M j \a\t h:i a');

See it in action

Anaesthetize answered 31/5, 2013 at 16:11 Comment(2)
Make sure not to use double quotes in date string, it could produce unexpected result.Alby
\t didnt work for me rather echo date('M j'). "at". date(' h:i a'); worked for me.Altogether
F
7

Try

<?php
    echo date('M j \a\t h:i a');
?>

OR

<?php
    echo date('M j'). "at". date(' h:i a');
?>
Faso answered 31/5, 2013 at 16:15 Comment(0)
D
5

You need to escape the t too:

echo date('M j \a\t h:i a');
Disharmony answered 31/5, 2013 at 16:11 Comment(0)
S
1

You need to try this in php:

$t = now(); //This will give you current time
echo date_format($t,"dS M,y \a\\t h:i a"); //14th May,19 at 05:39am
Symonds answered 27/5, 2019 at 18:45 Comment(0)
S
0

Another option could be:

echo date('M j')." at ".date('h:i a');
Sacristan answered 31/5, 2013 at 16:14 Comment(0)
D
0

This is working for me, if want to add "at" between date and time

date("d/m/Y \a\\t h:i A", strtotime($date_created)); // for string
date("d/m/Y \a\\t h:i A", $date_created); // for UNIX time
Doomsday answered 16/3, 2023 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.