I have datetime data in string format like this:
Sat Mar 24 23:59:59 GMT 2012
I want to convert this into a UTC timestamp, but when I try it as follows:
function texttotime($texttime)
{
if(!$texttime || $texttime=="")
return NULL;
// Sat Mar 24 23:59:59 GMT 2012
$bits = preg_split('/\s/', $texttime);
// Mar 24 2012 23:59:59 GMT
return strtotime("$bits[1] $bits[2] $bits[5] $bits[3] bits[4]");
}
It outputs 0 (not NULL).
If I change the last line to:
// Mar 24 2012 23:59:59
return strtotime("$bits[1] $bits[2] $bits[5] $bits[3]");
It outputs something (but the wrong timestamp, off by -4 hours or so).
Not quite sure why you're re-organising the existing string, as...
echo $timestamp = strtotime('Sat Mar 24 23:59:59 GMT 2012');
...works correctly. (It returns 1332633599, which you can check via date('r', 1332633599); (This will result in "Sat, 24 Mar 2012 23:59:59 +0000", so all is well.)
That said, if you're going to extract all of the components of the string, you might as well use mktime. For example:
function texttotime($texttime) {
if(!$texttime || $texttime=="") return NULL;
list($junk, $month, $day, $time, $timezone, $year) = explode(' ', $texttime);
list($hour, $minute, $second) = explode(':', $time);
return mktime($hour, $minute, $second, $month, $day, $year);
}
The reason you have 4 hour difference is caused by server timezone which is -4 hours from GMT.
try to define your current timezone like this:
date_default_timezone_set('Europe/London');
$result = strtotime('Sat Mar 24 23:59:59 GMT 2012');
echo date('r', $result);//Sat, 24 Mar 2012 23:59:59 +0000
Related
I want to convert the user's time, eg. 08:45 P.M, to UTC time zone. How can I do that?
if ($request->open_at)
{
$time = Carbon::parse($request->open_at)->toUTCString();
dd($time);
$query->whereTime('open_at', '>=', $time);
}
Like such, but unless you're always starting from the system timezone (configured in PHP), date must already have the correct timezone set for this to work, like others have mentioned.
$time = Carbon::parse($request->open_at);
$time->setTimezone('UTC');
...
Carbon extends the DateTime object including setTimezone
Use this PHP approach:
$time = new DateTime("08:45 P.M");
$time ->setTimezone(new DateTimeZone("UTC"));
echo $time ->format("Y-m-d H:i:s e");
User's time zone can be fed into the optional second argument of parse(). I've also been unable to find any toUTCString() method (🤔). All together:
$userTimeZone = 'Europe/Berlin'; // We'll come to this later
$time = Carbon::parse($request->open_at, $userTimeZone)->setTimezone('UTC');
echo $time->format('r');
For example:
foreach (['Asia/Tokyo', 'Europe/Berlin', 'America/Los_Angeles'] as $userTimeZone) {
echo "$userTimeZone\n";
$time = Carbon::parse('08:45 P.M', $userTimeZone);
echo $time->format('r'), "\n";
$time->setTimezone('UTC');
echo $time->format('r'), "\n\n";
}
Asia/Tokyo
Fri, 24 Dec 2021 20:45:00 +0900
Fri, 24 Dec 2021 11:45:00 +0000
Europe/Berlin
Fri, 24 Dec 2021 20:45:00 +0100
Fri, 24 Dec 2021 19:45:00 +0000
America/Los_Angeles
Thu, 23 Dec 2021 20:45:00 -0800
Fri, 24 Dec 2021 04:45:00 +0000
Of course, all of this is pointless if you don't know the user's time zone.
Do you have such information? The easiest way is to probably just ask, although you can also use some tricks to try and guess, such as:
Client-side JavaScript
Geolocation APIs
Carbon has ->utc() method (equivalent to setTimezone('UTC')) and Laravel query build can take Carbon object without having to format it as a string:
$query->whereTime('open_at', '>=', Carbon::parse($request->open_at)->utc());
Use the set timezone function to convert time
$time = Carbon::parse($request->open_at);
$time->setTimezone('UTC');
I'm from mail source I get dates like: Mon, 22 Jul 2013 09:08:50 +0200
I must get from this dates date: 2013-07-22.
I try this
$date = "Mon, 22 Jul 2013 09:08:50 +0200";
date("Y-m-d", strtotime($date))
What I do wrong ?
You need to echo the resulting value. Also, note that the output will be affected by the time zone on the machine running the PHP script.
Use like this
$date = "Mon, 22 Jul 2013 09:08:50 +0200";
echo date("Y-m-d", strtotime($date));
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a date:
Mon, 31 Mar 2014 12:19:10 GMT
How can I convert that to this format:
date('Y-m-d H:i:s')
I have found something like this:
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
But how do I convert 'Mar' to '03', so I can use that?
How about using DateTime
$date = new DateTime("Mon, 31 Mar 2014 12:19:10 GMT");
echo $date->format("Y-m-d H:i:s");
No need, PHP's strtotime understands month names.
C:\Users\Niet>php
<?php
var_dump(date('Y-m-d H:i:s',strtotime('Mon, 31 Mar 2014 12:19:10 GMT')));
^Z
string(19) "2014-03-31 12:19:10"
strtotime does the trick here too.
This code:
$date = 'Mon, 31 Mar 2014 12:19:10 GMT';
echo $date;
$time = strtotime('Mon, 31 Mar 2014 12:19:10 GMT');
echo date('Y-m-d H:i:s', $time);
will output this:
Mon, 31 Mar 2014 12:19:10 GMT
2014-03-31 14:19:10
Note that your timezone is important here too. I'm in GMT+2 timezone, so final hour is 14 instead of 12.
In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');
I have a date that is passed to PHP as such:
$date = mysql_real_escape_string($_POST["Date"]);
The date displays like this - Wed Sep 10 2013 00:00:00 GMT-0500 (EST)
Output needed - 2013-09-10
$dt = new DateTime('Wed Sep 10 2013 00:00:00 GMT-0500 (EST)');
echo $dt->format('Y-m-d');
See it in action
<?php
// $date = mysql_real_escape_string($_POST["Date"]);
// You should not use mysql_real_escape_string here.
$date = 'Wed Sep 10 2013 00:00:00 GMT-0500 (EST)';
echo date('Y-m-d', strtotime($date));
$date = mysql_real_escape_string($_POST["Date"]);
echo date('Y-m-d', strtotime($date));