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();
Related
I have a timezone date that have been posted to some PHP script.
like (Tue Dec 24 2013 00:00:00 GMT+0200 (Egypt Standard Time).
Now all what i need is simply convert that date to match mysql date which in "Y-m-d" format.
Is there any direct function? How to do that?
DateTime() will handle this easily.
$dt = new DateTime('Tue Dec 24 2013 00:00:00 GMT+0200');
echo $dt->format('Y-m-d');
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'));
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
I am parsing a xml, and I get the created time as: Wed, 24 Nov 2010 13:10:00 EST
My requirement is to convert this time into the specified time format using DateTime function before inserting it in the database.
Can anyone help me with this.
Thanks you
Zeeshan
set EST timezone
date_default_timezone_set('America/New_York');
convert to epoch
$epoch = strtotime('Wed, 24 Nov 2010 13:10:00 EST');
get date/time
echo date('Y-m-d H:i:s', $epoch);