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

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.

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

Try
<?php
echo date('M j \a\t h:i a');
?>
OR
<?php
echo date('M j'). "at". date(' h:i a');
?>

You need to escape the t too:
echo date('M j \a\t h:i a');

Another option could be:
echo date('M j')." at ".date('h:i a');

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

Related

php change the string into time format

In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.
A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output
You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php
Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );
Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.

convert date and time in php

i just need date format like this "26 March 2015 - 11:59 pm"
how to get 'pm'in my code
$month_year=date('F Y');
$timestamp = strtotime("$month_year");
$result = date('t F Y - 11:59 a', $timestamp);
Try below code
$date = date("d F Y - h:m a");
Just use DateTime like this:
$date = DateTime::createFromFormat("h:i a", "11:59 pm");
echo $date->format("d F Y - h:i a");
Output:
09 March 2015 - 11:59 pm
Check this
$month_year=date('F Y');
$timestamp = strtotime("$month_year");
echo $result = date('t F Y - H:i a', $timestamp);

How to append a string in date function first parameter?

I have following code
$date = '2013-05-11 07:10:14';
echo date('F j, Y at h:i a', strtotime($date); //Not work; May 11, 2013 at 7:10 am
echo date('F j, Y h:i a', strtotime($date); // This will work when avoiding the `at` from date function.
I am trying to append string in date function to display the above date as May 11, 2013 at 7:10 am. How to make date at this format using prebuilt date function?
Try escaping the at string,
echo date('F j, Y \a\t h:i a', strtotime($date));
DEMO.
You need to escape any letters that are also formatting queues. You have to double escape the "t" because \t is the tab character.
echo date('F j, Y \a\\t h:i a');
See it in action

PHP date conversion dd [th/st/rd] / month / yyyy

I have my users entering the date in this format :- mm/dd/yyyy (11/21/2012)
My PHP script converts the date into the following format :- dd-Month-yyyy (21-November-2012)
I do this using :-
$new_date = date('d-F-Y', strtotime($user_date));
How can I have the date in this format :- 21st November 2012?
Thanks
You can use S letter as following:
$new_date = date('jS F Y', strtotime($user_date));
Check manual.
It will output as you expect
$my_date = '2016-01-01';
echo date('F jS, Y', strtotime($my_date));
# January 1st, 2016
while dS will also prepends 0
echo date('F dS, Y', strtotime($my_date));
# January 01st, 2016
$new_date = date('jS F Y', strtotime($date));
S - English ordinal suffix for the day of the month, 2 characters
(st, nd, rd or th. Works well with j)
**My Date = 22-12-1992**
<?php
$mydate = "22-12-1992";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
**OutPut = 22nd December 1992**
You can use something like:
echo date('l, F jS');
Or even get a bit fancy with the HTML:
echo date('l, F j<\s\u\p>S</\s\u\p>');
You can do that :
<?php echo date('dS M. Y');?>
$date = date_create('09-22-2012');
echo $date->format('d S F Y');
by this code you will get your desire
for more you can also visit http://php.net/manual/en/datetime.formats.date.php

Date Format is not working

i have a string that is like this 2012/10/12 10:03:46 (Year/Month/Day) format now i want to change its format to something like this October 12, 2012 10:03 p.m i have tried php's DateTime class but its not working:
<?php
$date = new DateTime();
$date->createFromFormat('Y/m/dd H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('d-m, y h:i A');
?>
can anyone tell me whats wrong and how can i correct it??
You should have seen at least one notice something like "don't call static function in non-static context".
DateTime::createFomFormat()
$date = DateTime::createFromFormat('Y/m/d H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('F j, Y, g:i a');
It's a static method and (as the name suggests) it creates a new instance of DateTime.
$today = date("F j, Y, g:i a");
<?php
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('d-M, y h:i:s A');
//output - 12-Oct, 12 10:03:46 AM
?>
Try this...
$today = date("F j, Y h:i a"); // gives October 15, 2012 5:20 PM
Try this...
$today = date("F j, Y g:i A"); // gives October 15, 2012 5:20 PM (g removes leading zeroes)
Your case
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('F j, Y h:i A'); // gives October 15, 2012 10:03 AM
Reference : click

Categories