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
Related
I want to convert a date as below using php.
Mon Jun 17 2013 14:00:00 GMT 0300 (EEST)
When I run date("Y-m-d H:i:s",strtotime("Mon Jun 17 2013 14:00:00 GMT 0300 (EEST)")) the engine just generates 0300-06-17 15:56:56. If I update the code like date("Y-m-d H:i:s",strtotime("Mon Jun 17 2013 14:00:00 GMT")) it generates 2013-06-17 17:00:00. I have tried again and again but could not solve this issue. How Can I convert dates like this?
try to set the time zone and try
http://php.net/manual/en/function.date.php
Make sure you set your timezone like date_default_timezone_set('America/Los_Angeles') and try using gmdate().
http://us1.php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/function.gmdate.php
If you are trying to convert GMT to your timezone then you should convert both to a UNIX timestamp and do the math.
See:
http://us3.php.net/manual/en/function.date-timestamp-get.php
Please see the solution i posted over here. You can do the similar way to convert GMT to your desired timezone.
converting datetime to gmt
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
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
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');
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