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.
Related
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
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'm getting an external RSS Feed Post in different website, what I noticed is that they all have a different format of Publish Date.
Some uses a formatting like this:
2012-04-09T08:23:07Z
Fri, 06 Apr 2012 01:25:43 0000
There are still lots of format but I was just showing you two examples.
Now, I want everything to have a uniform format, so i'm using strtotime(). Here's my code:
$date = "2012-04-09T08:23:07Z";
date( "F d, Y", strtotime( $date ) );
/* Result: April 09, 2012 */
My problem here is the second format Fri, 06 Apr 2012 01:25:43 0000
There is a 0000 at the last which makes the strtotime() act weird.
$date = "Fri, 06 Apr 2012 01:25:43 0000";
date( "F d, Y", strtotime( $date ) );
/* Result: April 06, 0000 */
Obviously the result should be April 06,2012.
My question here is that how can I not let strtotime() act weird when it detects a format that has a 0000 at the last. Any idea how to do this? Your help would be greatly appreciated! Thanks! :)
Thanks!
I'm pretty sure you have a formatting issue:
Fri, 06 Apr 2012 01:25:43 0000
Notice the two spaces before 0000? That's not a coincidence. The original is probably this:
Fri, 06 Apr 2012 01:25:43 +0000
Which is generated with gmdate(DATE_RFC822). In URI's, the + symbol often gets translated to a space. I think that's your problem.
How about just stripping the "0000"? Something like:
$date=str_replace("0000", "", $date);
try this:
<?php
$date = "Fri, 06 Apr 2012 01:25:43 0000";
if (strtotime($date) !== false)
{
$timestamp = strtotime($date);
echo date( "F d, Y", $timestamp );
}
else
{
echo "error";
}
?>
for $date = "Fri, 06 Apr 2012 01:25:43 0000";
outputs error.
$date = "Fri, 06 Apr 2012 01:25:43";
gives output.
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.
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