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');
Related
Good day,
I have the following date that I get from my imap_fetch_overview PHP function :
$overview = imap_fetch_overview($inbox, $email_number, 0);
...
$date_event = $overview[0]->date;
This outputs a date like 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)'
In the meantime I have set my default timezone as following :
date_default_timezone_set('Europe/Brussels');
What I would like now is to be able to retrieve my $date_event in a format dd.mm.yyyy HH:mm:ss (local time, in my case 30.06.2017 18:27:44).
I have therefore tried :
$date_event_formated = $date_event('dd.mm.YYYY HH:ii:ss');
When calling 'echo $date_event_formated;', the only thing I get is a Fatal error.
So sorry if the question might sound silly, but I don't really understand what I am doing wrong here? Before bothering you, I looked at the reference website http://php.net/manual/fr/function.date.php but I may have missed something.
Thanks very much for your time and appreciated help.
While there is nothing wrong with Jakub's answer, using php 5.3 and up you can also use DateTime::createFromFormat, using an exact mask to parse the incoming date-string.
$oldDate = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
$date = DateTime::createFromFormat('D, d M Y H:i:s T e', $oldDate);
echo $date->format('Y-m-d');
strtotime() is able to handle the format that you have and convert it into timestamp. Then you can use it to format date in whatever format you want with date() function:
$date_event = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
echo date('d.m.Y H:i:s',strtotime($date_event));
//outputs: 30.06.2017 18:27:44
Here's working example:
https://3v4l.org/obfNB
use strtotime:
$date_event = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
echo date('d.m.Y H:i:s',strtotime($date_event));
This string 2016-09-13 08:56:55 +0000 - What is the time zone?
How do convert PDT time format Wed Oct 12 14:40:50 PDT 2016 to this foramt 2016-09-13 08:56:55 +0000.
I am try:
$input_datetime = 'Wed Oct 12 14:40:50 PDT 2016';
$result_datetime = date('Y-m-d H:i:s',strtotime($input_datetime)) . ' +0000';
echo $result_datetime; // 2016-10-12 14:40:50 +0000
I don't know about timezones. Just I'm making the datetime format.
Please update the solutions or suggest.
Thanks for every one!
Your expected output does not make sense to me, because line nogad mentioned, the seconds and minutes change. Please clarify. Meanwhile here the conversion format you wanted:
$datestr = 'Wed Oct 12 14:40:50 PDT 2016';
$dt = new DateTime($datestr);
echo $dt->format("Y-m-d\ H:i:s O");
Output is:
2016-10-12 14:40:50 -0700
I have the following date/time: Thu, 26 Jun 2014 07:13:32 +0000
I need to convert this to the format for inserting into mysql: 2014-06-26 07:13:32
How would I go about doing so?
I tried doing date("Y-m-d H:i:s", strtotime(Thu, 26 Jun 2014 07:13:32)) but it gives me the wrong time (specifically hour).
This code works fine for me and gives the result you specify above:
<?php
date_default_timezone_set("UTC");
print date("Y-m-d H:i:s", strtotime("Thu, 26 Jun 2014 07:13:32"));
Its because of the time zone causing the time to change try this:
<?php
date_default_timezone_set("UTC");
echo date("Y-m-d H:i:s",strtotime('Thu, 26 Jun 2014 07:13:32 +0000'));
You can convert the date to the desired format easily using DateTime class
$date = new DateTime('Thu, 26 Jun 2014 07:13:32 +0000');
$modified_date=$date->format("Y-m-d H:i:s");
echo $modified_date;
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));
I have a time value in javascript for example 11:30:00.
Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}
and I passed to a php file and converted it by:
$newTime = date('H:i:s', $theTime);
But, it return 05:36:00. What is the right way of concerting time?
Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.
If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
Which would output:
2012-10-22 04:30:00
This output is GMT, which you can change if required.
EDIT:
If you're expecting 11:30:00, then try the following:
date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));