I have date in the following format -
"Thu Jul 29 2021 17:47:12 GMT+0530 (India Standard Time)"
I want convert this date using php in the following format -
"2021-07-29T17:47:12"
I tried following ways so far but id didn't work -
//one way
$st = "Thu Jul 29 2021 17:47:12 GMT+0530 (India Standard Time)";
$st = strtotime($st);
$date = new DateTime($st, new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone('IST'));
//other way
$st = "Thu Jul 29 2021 17:47:12 GMT+0530 (India Standard Time)";
$st = strtotime($st);
$format = "Y-m-d";
$newdate = date_format(date_create($st), $format);
print_r($newdate);
There are many questions already available for date conversion on stack overflow itself but none of them answer my question as my date to convert is in different format so, please help me if anyone can. Thanks in advance.
You may have over-thought it.
Since you are ignoring any timezone differences, just truncate the characters at the end of string that are unnecessary then have strtotime() parse what is left.
Code: (Demo)
$date = 'Thu Jul 29 2021 17:47:12 GMT+0530 (India Standard Time)';
echo date('Y-m-d\TH:i:s', strtotime(substr($date, 0, 24)));
Output:
2021-07-29Z17:47:12
Another possible solution:
$date = 'Thu Jul 29 2021 17:47:12 GMT+0530 (India Standard Time)';
var_dump(DateTime::createFromFormat('D M d Y H:i:s+', $date)->format('Y-m-d\TH:i:s') );
Output
string(19) "2021-07-29T17:47:12"
PHP Documentation:
+
If this format specifier is present, trailing data in the string will not cause an error, but a warning instead
Related
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 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'm new at String functions, so I need a complex substr and trim functions for this string:
Wed, 28 Dec 2011 13:04:30 GMT
String comes to me always with this format. I want to convert it to DateTime object. Anybody can help me?
$dateString = 'Wed, 28 Dec 2011 13:04:30 GMT';
$dateTime = datetime::createfromformat('D, d M Y H:i:s e',$dateString);
echo $dateTime->format('d-M-Y H:i:s e');
<?php
$date = new DateTime('Wed, 28 Dec 2011 13:04:30 GMT');
echo $date->format('r');
... prints:
Wed, 28 Dec 2011 13:04:30 +0000
If you are wanting to take a date string and write that to the database as a date object using Doctrine:
Note: This is a form post example for Symfony 3 and 4.
$mynewdateobject = new \DateTime($request->request->get('mydatestring'));
Then you can write the object to the database or use it elsewhere in your code.
Which PHP function do I use to parse a date that is in the format "Fri, 28 Jan 2011 17:57:37 GMT"
strtotime doesn't seem to work for me on that date.
Thanks.
$date = date_create_from_format('D, j M Y H:i:s O', 'Fri, 28 Jan 2011 17:57:37 GMT')
Full docs on the format characters here.
Beats me why couldn't look this up yourself - you've got the function name in your post's title.
I have the following date and time in an RSS feed:
Fri, 01 Oct 2010 12:26:57 +0000
However I just want to display:
Fri, 01 Oct 2010 12:26:57
There should be a really simple way to do this in PHP, right?
If you don't mind showing the date in UTC/GMT (I forget which), then just use substring to strip off the +0000. However, if you want local time, you'll have to convert the string to a timestamp and then format the timestamp back to a date string.
$uncleandate = 'Fri, 01 Oct 2010 12:26:57 +0000';
$timestamp = strtotime($uncleandate);
$cleandate = date('D, d M Y H:i:s', $timestamp);
$clean_string = str_replace(" +0000", "", $your_date_string); should do the job
see str_replace doc