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.
Related
My vuexy datepicker returns the following string:
Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)
In my controller (Laravel) I want to convert this into simple date format - yyyy-mm-dd.
I have tried with strtotime() & date() but nothing works. It may need to use DateTime::createFromFormat() but I could not pick the syntax.
How can I convert it into yyyy-mm-dd?
This is working for me:
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date); # Get rid of TZ from the date string
echo date('Y-m-d', strtotime($date));
Here's another sample using the DateTime class
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date);
$dt = new DateTime($date);
echo $dt->format('Y-m-d');
Both Output:
2020-12-01
Hi Friend You Can Use Carbon Package one of the most recomonded package. Please follow the code as below
use Carbon; //use at the begining of class/controller
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$formated_date = Carbon::parse($date)->format('Y-m-d');
//output will be 2020-12-01
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
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 have a date that is passed to PHP as such:
$date = mysql_real_escape_string($_POST["Date"]);
The date displays like this - Wed Sep 10 2013 00:00:00 GMT-0500 (EST)
Output needed - 2013-09-10
$dt = new DateTime('Wed Sep 10 2013 00:00:00 GMT-0500 (EST)');
echo $dt->format('Y-m-d');
See it in action
<?php
// $date = mysql_real_escape_string($_POST["Date"]);
// You should not use mysql_real_escape_string here.
$date = 'Wed Sep 10 2013 00:00:00 GMT-0500 (EST)';
echo date('Y-m-d', strtotime($date));
$date = mysql_real_escape_string($_POST["Date"]);
echo date('Y-m-d', strtotime($date));
I've a date formatted like "Tue Jan 05 11:08:27 +0000 2010" and I want to convert it's format to "yyyy-mm-dd 00:00" in PHP.
How can I do that?
convert it to a PHP date object with strtotime() then output it with date()
EDIT
Some more detail; try:
$time = strtotime('Tue Jan 05 11:08:27 +0000 2010');
echo date("Y-m-d h:i", $time);
Y = 4 digit year
m = 2 digit month (with leading 0)
d = 2 digit month (with leading 0)
h = 12 hour time (leading 0)
i = minutes (with leading 0)
http://php.net/manual/en/function.date.php
for all the formatting options
$time_string = 'Tue Jan 05 11:08:27 +0000 2010';
$formated_time = date('Y-m-d h:i', strtotime($time_string));
echo $formated_time;
strtotime + date
Agree with Erik, if you want to do it in one line.
Solution
$date = date('Y-m-d H:i:s', strtotime('Tue Jan 05 11:08:27 +0000 2010'));