This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP convert one date into another date format
This is PHP
I have this result:
2011-09-20 13:00:00
I want to convert it into this format:
September 20 2011 1:00 pm
Do you know how to do that?
Anyhelp would be greatly appreciated.
Thanks!
try this:
$old_date = '2011-09-20 13:00:00';
$old_date_timestamp = strtotime($old_date);
$new_date = date('F j Y g:i a', $old_date_timestamp);
If that result comes from an object of type DateTime you can use the format function:
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
and here all the formats you can have.. change it according to your needs.
You can get the UNIX-timestamp of a time string with strtotime() and you can get a differently designed time string with strftime()
For more information you can read the documentation here : http://php.net/manual/en/function.date.php
But also is simple. Lets see how
$d = "2011-09-20 13:00:00";
$d = strtotime($d);
$d = date("F m Y g:i a", $d);
echo $d;
Take a look at these PHP functions:
http://nl.php.net/manual/en/function.date.php
and
http://nl.php.net/strtotime
To do something like this:
date('F d Y G:i',strtotime("2011-09-20 13:00:00")); // your required format
You can use this format
$date= date("F j Y g:i a");
if you have date in any variable then
$date= date("F j Y g:i a",strtotime($your_variable));
echo $date
echo date("F d Y g:i a",strtotime("2011-09-20 13:00:00 "));
echo date('F d Y g:i a', strtotime('2011-09-20 13:00:00'));
check the date format
Related
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.
I have $date = $run['at'];, which gives me 2013-06-03T16:52:24Z. How can I manipolate it to get for example "d M Y, H:i" ?
You should use the DateTime class.
$date = new DateTime($run['at']);
echo $date->format('d M Y, H:i');
You can use:
$time = strtotime($run['at']);
echo date("Y-m-d", $time);
But, where does $run['at'] come from?
PHP code:
echo date("c"); //out put like this 2012-06-19T20:37:44+05:30
echo date("d M, Y"); //out put "Jun 19,2012"
$time=date("c");
echo date("d M, Y",$time);// This is not working. What could the reason be?
My requirement is to convert ISO8601 format to 'd M ,Y'. I need it in this format to use with the timeago jQuery plugin.
You need to use the strtotime() function.
echo date("d M, Y",strtotime(date("c")));
Alternatively, you can use PHP's DateTime object to perform the conversion:-
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, date('c'));
echo $dateTime->format('d M Y');
The issue is that the second argument to date is expected to be a timestamp, you are passing a string. Use strtotime to convert your date to into a timestamp:
$time = date("c");
echo date("d M, Y", strtotime($time));
I have dates currently in this format: yy-mm-dd (e.g. 2011-11-18)
I want them in this format: Friday 18 November 2011
I've tried reading through the PHP documentation manual, but I can't see how to manage dates in the format that I have. If the date needs to be in a different order I can arrange that, but I'm a bit stuck at the meoment.
Any help would be much appreciated.
Use PHP5s new date classes. Much cleaner:
$date = DateTime::createFromFormat('Y-m-d', '2011-11-18');
echo $date->format('l d F Y');
date('l j F Y', strtotime($date));
Just use starttime to change the the dates in many formats using this link.
echo date('l d F Y');
gives you the date format you want.
This was all in the manual you yourself linked.
just use strtotime to get back a timestamp and then use date() to format that:
$date = '2011-11-18'; // your date
$timestamp = strtotime($date); // convert to a timestamp
$new_date = date('l j F Y',$timestamp) // format timestamp
echo $new_date;
This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM