Convert a human date to unix timestamp - php

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

Related

Convert date string to unix timestamp

I have a string:
Mon Jul 07 2014 13:47:03 GMT+0800 (Malay Peninsula Standard Time)
I want to convert to unix timestamp in PHP.
However using strtotime() does not work for me. Is there any other work around so I can get the unix timestamp from this string?
First, strip (Malay Peninsula Standard Time) because you have one more time the timezone on GMT+0800, you can clean your datetime string with substr like
$dateString = "Mon Jul 07 2014 13:47:03 GMT+0800 (Malay Peninsula Standard Time)";
$dateString = substr($dateString, 0, strpos($dateString, '('));
after remove this you can use DateTime Object like
$date = new DateTime($dateString); //Mon Jul 07 2014 13:47:03 GMT+0800
echo $date->getTimestamp();

Converting timestamps in php

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

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

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

PHP date format conversion

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

Categories