I'm receiving a string containing date and time (in UTC format) for when something was created. The string looks like this: "Wed Mar 13 14:10:20 +0000 2013". Now, I need to convert that to a more readable format. Something like this "14:10, 13 Mar" or preferably "1 hour ago", "1 week ago" etc.
How do I do this?
Thanks.
Use the DateTime object. The constructor should be able to parse that timestamp, then you can mainpulate the date and output in any format you wish:
$date = new dateTime('Wed Mar 13 14:10:20 +0000 2013');
echo $date->format('h:i, d-M');
For relative times, see: Convert 2010-04-16 16:30:00 to "Tomorrow Afternoon"
Try this:
$dt = 'Wed Mar 13 14:10:20 +0000 2013';
echo date( 'h:i, d-M', strtotime( $dt ) );
The time difference will require more code.
Related
I need a way to convert this format Tue, 20 Oct 2015 17:43:23 0000
to the datetime format that MySQL accepts.
I have tried to convert it with strtotime() but the function returns a negative number.
The problem appears to be the trailing zeroes, by trimming them from the date string it seems to work fine. The code below uses one of the Date constants - change that for the format desired.
$d='Tue, 20 Oct 2015 17:43:23 0000';
echo date( DATE_COOKIE, strtotime( trim($d,' 0000') ) );
outputs -> Tuesday, 20-Oct-15 17:43:23 BST
echo date( 'Y-m-d H:i:s', strtotime( trim($d,' 0000') ) );
outputs -> 2015-10-20 17:43:23
Try this with your time specified as the $inputtime variable:
strftime("%F %H:%M:%S", strtotime($inputtime))
The problem lies in your time format Tue, 20 Oct 2015 17:43:23 0000. Here 0000 means time zone. But it should have format + or - before value to work properly. Look at example:
echo strtotime("Tue, 20 Oct 2015 17:43:23 +0000");
Try this
$date = \DateTime::createFromFormat('r',your date);
$date = $date->format(format that you need);
I am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600
You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);
You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));
I am getting a date format like Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time) form a javascript to my php function.
I want to store that to my database table in "2013-04-22 12:16:00" format.
Can any one help me to convert this date type.
I tried:
date("Y-m-d H:i:s",$startDate);
But it's giving error as
date() expects parameter 2 to be long string given
Use strtotime() and date():
$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
or use DateTime:
<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>
DEMO
Try using strtotime to convert the timestamp to unix-format so you can use it in the date() function:
date("Y-m-d H:i:s",strtotime($startDate));
You can also try using the (usually included) DateTime class. In particular, have a look at DateTime::createFromFormat. It may help you get around ambiguities in the date string (strtotime() will sometimes fail or mis-parse a date string). DateTime::createFromFormat allows you to specifically designate the format of the date string so there can be no ambiguity.
firstly you need to save it as Timestamp so,
$startDate = strtotime($javascript_value);
$result = date("Y-m-d H:i:s",$startDate);
I have a time value in javascript for example 11:30:00.
Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}
and I passed to a php file and converted it by:
$newTime = date('H:i:s', $theTime);
But, it return 05:36:00. What is the right way of concerting time?
Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.
If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
Which would output:
2012-10-22 04:30:00
This output is GMT, which you can change if required.
EDIT:
If you're expecting 11:30:00, then try the following:
date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
How to convert "00:00:00.000 GMT Mon Nov 22 2010" to more user friendly format.
You could use these PHP functions:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php
strtotime will convert your current to a unix timestamp.
You can then format that unix time stamp however you want, using date().
Why not with strtotime?!
echo date('d/m/Y',strtotime('0:00:00.000 GMT Mon Nov 22 2010')); // I'm french
You might also find these useful:
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $mysqldate );
And in CakePHP the TimeHelper http://book.cakephp.org/view/1470/Time:
echo $time->nice($mysqlDate);
etc.
Try this:
//initial format
echo '0:00:00.000 GMT Mon Nov 22 2010';
//unix format: 2010-11-22
echo date('Y-m-d',strtotime('0:00:00.000 GMT Mon Nov 22 2010'));
//explicit format: Monday 22 November 2010
echo date('l d F Y',strtotime('0:00:00.000 GMT Mon Nov 22 2010'));
First convert the string to a datetime using strtotime:
strtotime
and then format the datetime anyway you want using sprintf:
sprintf
Use strtotime function:
echo strtotime("00:00:00.000 GMT Mon Nov 22 2010"), "\n";
check on php manual and try strtotime and date function