Convert a datetime format to match MySQL's datetime with PHP - php

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);

Related

What create_from_format to use for timestamps in php

I am working on a PHP file were I am pulling in timestamps from a database that come out as
Tue Sep 22 16:11:12 EDT 2015
Mon Nov 16 07:42:31 EST 2015
I was wondering how to convert dates of this format into unix time format.
You can use the DateTime object
$dateTime = new DateTime('Tue Sep 22 16:11:12 EDT 2015');
echo $dateTime->getTimestamp();
The result would be 1442952672
If you look at the PHP manual for \DateTime::createFromFormat() or date(), you will find tables of codes accepted by \DateTime::createFromFormat(). Looking at them you will see that you need the following:-
$date = \DateTime::createFromFormat('D M d H:i:s T Y', 'Tue Sep 22 16:11:12 EDT 2015');
echo $date->getTimestamp();
Demo.
The 'd' in the format string may need to be a 'j' if day numbers < 10 are single digit.

PHP Change 24 hour datetime using strtotime

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)));

Convert string of UTC time to more readable format

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.

Javascript time to PHP time

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));

String to time PHP

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

Categories