Parsing Twitter API Datestamp - php

I'm using the twitter API to return a list of status updates and the times they were created. It's returning the creation date in the following format:
Fri Apr 09 12:53:54 +0000 2010
What's the simplest way (with PHP or Javascript) to format this like 09-04-2010?

Cross-browser, time-zone-aware parsing via JavaScript:
var s = "Fri Apr 09 12:53:54 +0000 2010";
var date = new Date(
s.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,
"$1 $2 $4 $3 UTC"));
Tested on IE, Firefox, Safari, Chrome and Opera.

strtotime("dateString"); gets it into the native PHP date format, then you can work with the date() function to get it printed out how you'd like it.

JavaScript can parse that date if you remove the +0000 from the string:
var dStr = "Fri Apr 09 12:53:54 +0000 2010";
dStr = dStr.replace("+0000 ", "") + " UTC";
var d = new Date(dStr);
Chrome -- and I suspect some other non IE browsers -- can actually parse it with the +0000 present in the string, but you may as well remove it for interoperability.
PHP can parse the date with strtotime:
strtotime("Fri Apr 09 12:53:54 +0000 2010");

Here is date format for Twitter API:
Sat Jan 19 20:38:06 +0000 2013
"EEE MMM dd HH:mm:ss Z yyyy"

Javascript. As #Andy pointed out, is going to be a bitch when it comes to IE. So it's best to rely on a library that does it consistently. DateJS seems like a nice library.
Once the library is added, you would parse and format it as:
var date = Date.parse("Fri Apr 09 12:53:54 +0000 2010");
var formatted = date.toString("dd-MM-yyyy");
In PHP you can use the date functions or the DateTime class to do the same (available since PHP 5.2.0):
$date = new DateTime("Fri Apr 09 12:53:54 +0000 2010");
echo $date->format("d-m-Y"); // 09-04-2010

FYI, in Java it is:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date d = sdf.parse(dateAsString);
sdf = new SimpleDateFormat("dd-MM-yyyy");
String s = sdf.format(d);

And in Swift:
let dateString = "Fri Apr 09 12:53:54 +0000 2010"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
let date = dateFormatter.date(from: dateString)

Related

Date conversion in PHP

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

Formatting date and time to get date, month, year each in seperate var

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

String to DateTime Object

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.

PEAR Date date_create_from_format

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.

PHP - Transform Date

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

Categories