How to transfer DATE_ATOM to unix time? - php

some time format like: Sat, 19 Feb 2011 09:02:04 -0800,
How to transfer the DATE_ATOM to unix time(a ten digits numbers)? thanks.

$timestamp = strtotime('Sat, 19 Feb 2011 09:02:04 -0800');
http://www.php.net/manual/en/datetime.formats.php
http://php.net/manual/en/function.strtotime.php

Try this
strtotime('Sat, 19 Feb 2011 09:02:04 -0800');

Related

human to UNIX time conversion with PHP

I have this strings like this: Thu, 27 May 2010 07:00:00 GMT
I want to convert them to UNIX timestamp
Is there any way?
echo strtotime('Thu, 27 May 2010 07:00:00 GMT');
http://php.net/manual/en/function.strtotime.php
Yes, there is a way! In fact, there is more than one way to do it! ;)
You can use strtotime();
$time_en = "Thu, 27 May 2010 07:00:00 GMT"
$time = strtotime($time_en);
http://fr2.php.net/manual/fr/function.strtotime.php

Convert a human date to unix timestamp

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

PHP date/strtotime converting to incorrect date

On some occasions data() is converting my dates incorrectly.
My date formats look like so:
Fri Oct 25 15:00:00 EDT 2011
The date string comes from an external source, so I'm unable to change the format.
// output incorrect - Fri, 28 Oct 2011 15:00:00 -0400
date("r", strtotime("Fri Oct 25 15:00:00 EDT 2011"))
// output correct - Fri, 21 Oct 2011 15:00:00 -0400
date("r", strtotime("Fri Oct 21 15:00:00 EDT 2011"))
I can't figure out why just changing the day makes it fail to convert.
My end goal is to create a DateTime object but it suffers from the same problem.
// output - Fri Oct 28 19:00:00 EDT 2011
DateTime("Fri Oct 22 19:00:00 EDT 2011")
Oct 25/2011 is a tuesday. Strtotime is interpreting your date string as "what's the next friday AFTER Oct 25th", and is returning (what it thinks) is the correct answer: October 28th.
So, the GIGO rule applies. You're feeding in garbage, and wondering why you're getting garbage out.
The value you're passing into strtotime doesn't tally. There's no Friday 25th, 2011.
http://www.timeanddate.com/calendar/monthly.html
date() works fine.
Problem is, in your examples with incorrect output specified date is not Friday.
You can strip out first 4 characters if it always is Fri.

strtotime date weird result

Given the following string date: Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)
in php if I do a strtotime on the above, and then convert it back to a string date, it seems to gain an hour.
echo $str_date," Vs ",date("c",strtotime($str_date));
Produces:
Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time) Vs 2011-09-02T22:00:00+01:00
I realise this is to do with daylight savings, but how does one compensate for this?
I think you misunderstanding,
there is not day light saving in this case,
BUT GMT, you gain one hour because of that
in my timezone (GMT+8)
php -r "echo date('r', strtotime('Fri Sep 02 2011 21:00:00 GMT+0100'));"
Sat, 03 Sep 2011 04:00:00 +0800
which I gain 7 hours, due to GMT+8 - GMT+1 = 7
I realise this is to do with daylight savings, but how does one compensate for this?
By not using date() and strtotime(); the DateTime class is preferred.
$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
$datetime = new DateTime($str_date);
echo $datetime->format('c'); // 2011-09-02T21:00:00+01:00
or in procedural style
$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo date_format(date_create($str_date), 'c'); // 2011-09-02T21:00:00+01:00
Aside: if you wish to still use date()/strtotime() then, as the other answers and your own observations show, you need to be careful with the time zones in use in the date string and your script.
Which PHP version do you use? What is your date.timezone setting? I'm asking because I cannot reproduce your output running PHP 5.3.6 on Mac OS X:
$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)';
echo $str_date," Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time) Vs 1970-01-01T01:00:00+01:00
This is correct because Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time) is not a valid date/time string.
$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo $str_date," Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100 Vs 2011-09-02T22:00:00+02:00
This is correct because I'm in GMT+2.
Seems like strtotime() renders your time as SOAP format: YY "-" MM "-" DD "T" HH ":" II ":" SS frac tzcorrection?
result is:
"2008-07-01T22:35:17.02", "2008-07-01T22:35:17.03+08:00"
You can try to format your time string as some other time format. Look in http://www.php.net/manual/en/datetime.formats.compound.php

date() returning wrong day although the timestamp is correct!

I have a bizzare problem with php date function.
code:
$numDays = 8;
$date = strtotime('2010-11-06');
for ($i=1; $i<=$numDays; $i++)
{
$thisDay = date("D, d M Y", $date);
print ($thisDay.'<br>');
$date+=86400; // add one day to timestamp
}
result on my server (local host, windows):
Sat, 06 Nov 2010
Sun, 07 Nov 2010
Mon, 08 Nov 2010
Tue, 09 Nov 2010
Wed, 10 Nov 2010
Thu, 11 Nov 2010
Fri, 12 Nov 2010
Sat, 13 Nov 2010
Result on my web server (linux)
Sat, 06 Nov 2010
*Sun, 07 Nov 2010
Sun, 07 Nov 2010*
Mon, 08 Nov 2010
Tue, 09 Nov 2010
Wed, 10 Nov 2010
Thu, 11 Nov 2010
Fri, 12 Nov 2010
Notice how Sun, 07 Nov 2010 appears twice on the remote server?? Why is this happening? can anyone explain this Behavior?
November 7, 2010 is the DST switch date in many time zones (but not Greece where you seem to be located). From Wikipedia:
Starting in 2007, most of the United States and Canada observe DST from the second Sunday in March to the first Sunday in November, almost two-thirds of the year.
In Greece, it seems to be October 31. Which time zone do you have you set on your machine?
It's good practice to do time calculations in UTC and then convert them to the required timezone for the user's location using PHPs datetime functions:
date_default_timezone_set('UTC');
$timezone = new DateTimeZone('Europe/Athens');
$datetime = new DateTime('now', $timezone);
echo $datetime->format('Y-m-d H:i:s');
It's hard to be certain here, but could you problem be caused by a daylight saving time transition in the remote server's timezone?
In most countries the transition usually happens on weekends and, since the day is extended by an hour, this is one case where adding 86400 seconds to a time value would not return the date after that.
Run your code with the starting date +1 hr and then -1hr and see what results you get. You'll get more clues and most likely it is to do with Daylight Saving Time.
Also, as pointed out by Pekka, try the same with the date set to Oct 31st and see what happens.
Good question.

Categories