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));
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');
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 am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600
You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);
You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));
I am located in the (PDT) time zone at time Sat May 11 2013 20:58:51 (my time) I generated a date/time using the following code.
var date = new Date();
alert(date);
This returns the result
"Sat May 11 2013 20:58:51 GMT-0700 (PDT)"
If I then post this date to a php script which processes it in the following way:
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
I get the result
"2013-05-12 03:58:51"
Not what I expected. I only get the expected time if I get rid of the "GMT-0700 (PDT)" part from my date/time. So I have two questions.
Can anyone tell me how to generate a date of this format but with out the "GMT-0700 (PDT)" part in javascript without using string functions/regex?
Is my browser giving me the wrong time zone, or is php interpreting the time zone incorrectly. In either case, why?
JavaScript always take your local server timezone, while PHP is converting your date to UTC, so you need to set your server time zone before using strtotime as follow,
date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
DEMO.
Or if you would like to convert your js date to UTC than use,
var date = new Date();
var utcdate = date.toUTCString()
$date = date('r', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
1- echo date("l F j, Y, H:i s");
2-
date_default_timezone_set('America/Los_Angeles');
$postedDate = "Sat May 11 2013 20:36:24 GMT-0700 (PDT)";
$date = date('Y-m-d H:i:s ', strtotime($postedDate));
echo json_encode($date);
I have the following date and time in an RSS feed:
Fri, 01 Oct 2010 12:26:57 +0000
However I just want to display:
Fri, 01 Oct 2010 12:26:57
There should be a really simple way to do this in PHP, right?
If you don't mind showing the date in UTC/GMT (I forget which), then just use substring to strip off the +0000. However, if you want local time, you'll have to convert the string to a timestamp and then format the timestamp back to a date string.
$uncleandate = 'Fri, 01 Oct 2010 12:26:57 +0000';
$timestamp = strtotime($uncleandate);
$cleandate = date('D, d M Y H:i:s', $timestamp);
$clean_string = str_replace(" +0000", "", $your_date_string); should do the job
see str_replace doc