how to convert Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time) this format to mysql date format using PHP?
I tried to use strtotime but none of them working for me
date("Y-m-d H:i:s", strtotime("Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)"));
The date fails to parse because it's in a very weird format.
If you can't control the format of the incoming date you could grab the different parts using regex and parse them:
$rawDate = "Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)";
preg_match('/(.*?) GMT (\d+)\s\(.*?\)/', $rawDate, $matches);
$date = DateTime::createFromFormat('D M j Y H:i:s', $matches[1])
->setTimezone(new DateTimeZone('+' . $matches[2]));
echo $date->format('Y-m-d H:i:s'); // outputs 2019-07-19 06:00:00
Example: https://3v4l.org/fbuMk
Related
I am passing jQuery date in backend. date is Thu Jun 01 2017 00:00:00 GMT 0530 (India Standard Time).
I want to convert this date in 2017-06-19 this format but in php.
$date_str='Thu Jun 01 2017 00:00:00 GMT 0530 (India Standard Time)';
echo date_format(date_create_from_format('Y-m-d', $date_str), 'd-m-Y');
I have tried this solution but it is not working for me.
You can use this code:
$timezoneDataStartPos = strpos($date_str," GMT");
$formattedString = substr($date_str, 0, $timezoneDataStartPos);
$formattedDate=DateTime::createFromFormat('D M d Y H:i:s', $formattedString, new DateTimeZone('Asia/Kolkata'));
echo $formattedDate->format('Y-m-d');
You need to remove " GMT 0530 (India Standard Time)" and make sure to preserve Timezone as Asia/Kolkata
Try this code:
Note : Remove 0530 (India Standard Time) from your date format and use date format shown below my code.
<?php
$date_str='Thu Jun 01 2017 00:00:00 GMT ';
echo date('Y-m-d',strtotime($date_str));
?>
I have this format :
Thu Feb 11 2010 00:00:00 GMT+0100 (CET)
My Question :
How can i format this date, to this kind of format in PHP:
2010:01:01 00:00:00 (Datetime)
$date = new \DateTime('Thu Feb 11 2010 00:00:00 GMT+0100 (CET)');
$formatedDate = $date->format('Y:m:d H:i:s');
See https://3v4l.org/TfR85 and for more information on the datetime object http://php.net/manual/en/book.datetime.php
I must also point out that your initial string doesn't follow any proper format. CET or GMT... there is a big difference
Using strtotime():
$s = strtotime("Thu Feb 11 2010 00:00:00 GMT+0100 (CET)");
$s = date("Y-m-d H:i:s",$s);
echo $s; //2010-02-11 00:00:00
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.
Currently I'm using jquery datepicker that support date and time.
The return value is string
return value
"Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)"
I wan to convert the above string to php date time to store into database .
I just need date time and timezone .
First you have to remove information, that breaks conversion:
preg_replace('/( \(.*)$/','','Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)');
Results in:
'Fri Apr 10 2015 12:00:00 GMT+0800'
what you can convert to timestamp using strtotime():
$datetime = "Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)";
$datetime = preg_replace('/( \(.*)$/','',$datetime);
$timestamp = strtotime($datetime);
$date = date('Y-m-d H:i:s \G\M\TP', $timestamp);
I'm GMT+2 so I receive 2015-04-10 06:00:00 GMT+02:00. Further formatting depents on your desired date() formatting.
If you want to keep original time, heres the trick:
$datetime = "Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)";
$datetime = preg_replace('/( \(.*)$/','',$datetime);
$date = new DateTime($datetime);
$zone = $date->getTimezone();
echo $date->format('Y-m-d H:i:s') . ' GMT' . $zone->getName();
gives 2015-04-10 12:00:00 GMT+08:00.
In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');