Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 219
Sunil Kumar Avatar asked May 31 '13 16:05

Sunil Kumar


People also ask

Is date and time a string?

A date and time format string is a string of text used to interpret data values containing date and time information. Each format string consists of a combination of formats from an available format type. Some examples of format types are day of week, month, hour, and second.

How can I add the one day with date in PHP?

?> Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds. Syntax: date_add(object, interval);


3 Answers

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

like image 85
John Conde Avatar answered Sep 19 '22 14:09

John Conde


Try

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

OR

<?php
    echo date('M j'). "at". date(' h:i a');
?>
like image 29
Vijaya Pandey Avatar answered Sep 17 '22 14:09

Vijaya Pandey


You need to escape the t too:

echo date('M j \a\t h:i a');
like image 32
Nelson Avatar answered Sep 18 '22 14:09

Nelson