I got time stamp in this format from twitter api.
Fri Dec 28 20:06:38 +0000 2012
I want to convert this to standard time stamp format like this one.
2012-12-10 16:20:18
Am pretty new to dates in php. How can I do it??
You can use DateTime:
$date = new DateTime('Fri Dec 28 20:06:38 +0000 2012');
echo $date->format('Y-m-d H:i:s');
The reason why I prefer DateTime is that it gives great oop implementation and makes it easier to work with dates that are quite big head-ache of programmer. For more information about this class read the manual that I have already referenced.
You can use strtotime to convert the initial string to a UNIX timestamp and then strftime to convert it back to a string:
strftime('%Y-%m-%d %H:%M:%S', strtotime('Fri Dec 28 20:06:38 +0000 2012'));
That call returns the string 2012-12-28 21:06:38 - i.e. exactly what you are looking for.
use this
$originalDate = "Fri Dec 28 20:06:38 +0000 2012";
echo $newDate = date("Y-m-d h:i:s", strtotime($originalDate));
working example http://codepad.viper-7.com/AhLcEU
In case of use php < 5.2.0 (some shared hostings) use a combination of strtotime and date
date('Y-m-d H:i:s', strtotime('Fri Dec 28 20:06:38 +0000 2012'));
Related
Through the twitter API I was able to get the datetime of when something was tweeted
Jul 25 17:42:55 +0000 2013
Now, in PHP, how do I get that into standard unix:
2013-6-25 17:42:55
I'm not to sure on anything to deal with datetime but I think there is an easier way to do this rather than having to parse through and change things with str_replace and substr
Use the DateTime class, specifically the static createFromFormat() method
$dt = DateTime::createFromFormat('M j H:i:s P Y', 'Jul 25 17:42:55 +0000 2013');
echo $dt->format('Y-m-d H:i:s');
Working example - http://codepad.viper-7.com/gLdEll
Simply pass it through strtotime. Note that this includes a timezone +0000 so the time will translate relative to your timezone also.
<?php
$date = "Jul 25 17:42:55 +0000 2013";
echo date("Y-m-d H:i:s", strtotime($date));
I have a jQuery script that returns a date as:
Wed Nov 09 2011 16:30:00 GMT-0700 (MST)
How could I convert that into unix timestamp? I was looking at mktime() but I'm not really understanding it completely. Any ideas?
If you're using PHP 5.2, try the DateTime class, eg
$dt = new DateTime("Wed Nov 09 2011 16:30:00 GMT-0700 (MST)");
$ts = $dt->getTimestamp();
Otherwise, try strtotime(), eg
$ts = strtotime("Wed Nov 09 2011 16:30:00 GMT-0700 (MST)");
echo date("r", $ts);
For me, this outputs
Thu, 10 Nov 2011 10:30:00 +1100
Note that the date() function is local timezone aware
I take it that jQuery's using a Date object; instead, have the script send the value of Math.floor(theDate.getTime() / 1000) to your PHP script. That's the Unix timestamp you need.
What about strtotime ?
$test = strtotime('Wed Nov 09 2011 16:30:00 GMT-0700 (MST)');
echo $test;
output : 1320881400
What is the equivalent of javascript new Date(2011, 3, 1) in PHP?
p.s. the output of the function above is:
Fri Apr 01 2011 00:00:00 GMT+0100 (BST) {}
I have tried looking trough php DateTime Predefined Constants, but didn't find similar patern.
Try using this:
echo date ("M-d-Y", mktime (0,0,0,12,32,1997));
More information can be found here: mktime function manual
You just have to use proper values in date("XXX") function. More info can be found here: date function manual
If you prefer Greenwich Mean Time (GMT) you should use: gmdate
<?php
$date = date_create('2011-03-01');
echo date_format($date, 'D M d Y H:i:s eO (T)');
?>
Formatting: http://www.php.net/manual/en/function.date.php
If you want the RFC2822 standardized formatting, use r for the format. That would give you the format:
Thu, 21 Dec 2000 16:01:07 +0200
<?php
date("D M d Y H:i:s \G\M\TO (T)",strtotime('Midnight April 1st 2011'));
?>
Which would output Fri Apr 01 2011 00:00:00 GMT+0100 (BST) if your timezone is set correctly (in this case to Europe/London)
Or use mktime() or time() or a timestamp or what have you instead of strtotime(). Note that eO (T) in the date format won't give the same output as the java function, as if the timezone is North America/New York or whatever, then it would output something like North America/New York-0500 (EST) Rather than GMT-0500 (EST)
Can also use a DateTime object.
I have a variable in php:
$d = "19:02:13 Nov 07, 2010 PST";
What is the quickest way to convert the format so I get (mysql DATETIME):
$d = "2010-11-07 19:02:13";
try this
date_default_timezone_set("America/Los_Angeles");
echo date("Y-m-d H:i:s", strtotime("19:02:13 Nov 07, 2010 PST"));
As easy (though not epically efficient) way would be to use strtotime as follows:
date('Y-m-d H:i:s', strtotime('19:02:13 Nov 07, 2010 PST'))
However, make sure you've set the correct local timezone via date_default_timezone_set or this will not work correctly.
Need help in date conversion:
Existing date format:
Wed, 01 Dec 2010 00:00:00 -0500
(Day, DD MMM YYYY HH:MM:SS GMT-0500)
To be changed to:
2010-11-29T04:59:59-05:00
(YYYY-MM-DD(T)HH:MM:SS GMT-05:00)
How to handle in PHP?
is there any function available in php for this.
please help
strtotime() (man page) & date() (man page) or DateTime class (man page) should be able to handle this.
echo date('Y-m-d\TH:i:sZ', strtotime('Wed, 01 Dec 2010 00:00:00 -0500'));
echo date('c', strtotime('Wed, 01 Dec 2010 00:00:00 -0500')); // as mentioned by Anthony
or
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('Y-m-d\TH:i:sZ');
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('c'); // as mentioned by Anthony
First you want the date string in epoch format, so that you can use the date function. My favorite method to do this is the strtotime function:
$epoch_date = strtotime($original_date_string);
Now, you can use the date function to output it however you like. In your case, I believe you are looking for ISO 8601, which is built into the function:
$new_date_string = date('c', $epoch_date);
echo $new_date_string;
date('Y\-m\-d\Th:i:s \G\M\TP');
This will return:
2010-11-26T02:49:24 GMT-05:00
Use the date() formating its much simpler!
You can read all about it right here http://php.net/manual/en/function.date.php