The server's locale is set correctly, on other domains it seems to be fine... however I am currently experiencing a strange problem.
If I do a straight strtotime('now'); it returns the right timestamp (for today's date/time in my timezone) however if I do:
strtotime('07/09/2012 13:48');
It returns a timestamp that's for 9th July, like it's reading the date as a US format. I've retrieved the timezone and it is set to Europe/London (by using date_default_timezone_get).
Any ideas?
$date = DateTime::createFromFormat('d/m/Y H:i', '07/09/2012 13:48');
echo $date->format('Y-m-d H:i');
http://www.php.net/manual/en/datetime.createfromformat.php
Use the DateTime class and DateTime::createFromFormat() method.
strtotime reads it as the US format. Best to use the ISO yyyy-mm-dd.
See this for valid date formats
Related
I am trying to convert datetime to a UTC timecode using PHP, how do I convert 2022-05-14 13:30:30 so it will come out as 2022-05-14T13:30:30+01:00
Thanks
The format can be obtained with the method toAtomString(). With my timezone settings, i got the timezone 02:00, so i had to use setTimezone() and specifying the timezone on the parsing. Can be left out, if yours is correct.
Carbon::parse('2022-05-14 13:30:30', 'Europe/london')
->setTimezone('Europe/london')
->toAtomString();
This will format the date as follows 2022-05-14T13:30:30+01:00.
Simple solution with DateTime:
$date = date_create('2022-05-14 13:30:30', new DateTimeZone('Europe/London'))
->format(DateTimeInterface::ATOM);
echo $date; //2022-05-14T13:30:30+01:00
Try 3v4l.org.
I have a lot of variables of dates and times and I need to setTimezone in an abstract file. So I don't know anything about what is my times or dates format.
How I can keep the same format after changing the timezone? Or is there any way to find what is my date and time format?
For example, I have a timestamp like Y-m-d H:i and in my abstract class I'm changing the time zone by Carbon::parse($timestamp)->setTimezone($userTimezone) but with this line of code I'm losing my timestamp format and this code should convert multi-format of date and times. Another timestamp may have a format like Y/m/d H:I:s
I need something like this:
$format = Carbon::parse($timestamp)->getFormat(); // Returns 'Y-m-d H:i'
Or this can help; How I can setTimezone of a string timestamp without changing the format? date_default_timezone_get and date_default_timezone_set is not a good idea for this question.
You can simple change to:
Carbon::parse($timestamp)->setTimezone($userTimezone)->format('Y-m-d H:i');
I am not sure how to convert timestamp into ISO-8601 and also apply UTC.
However, I am looking for following output:
2017-09-23T01:08:36.6437128Z ( Format = ISO-8601. Timezone = UTC)
I have tried couple of ways to get this but I am not getting it so anyone know how to get this. As this must be very quick and easy fix but I am missing something and that's the reason I am not getting this output.
Any help would be appreciated:)
Thanks
First of all, your desired format is not ISO 8601 compliant because you're including microseconds.
Use date_default_timezone_set to get UTC and date to easily format a timestamp.
Demo: https://3v4l.org/aRKqi
$timestamp = 1506086214;
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// ISO 8601 (does NOT include microseconds)
echo date("c", $timestamp) . PHP_EOL;
// custom format e.g. 2017-09-23T01:08:36.6437128Z
echo date("Y-m-d\TH:i:s.u\Z", $timestamp) . PHP_EOL;
2017-09-22T13:16:54+00:00
2017-09-22T13:16:54.000000Z
Paypal returns a timestamp of the following format:
yyyy-MM-ddTHH:mm:ssZ
And I don't quite know what to do with it...
How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?
I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.
Use DateTime class to do your magic.
$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.
Example with date():
echo date('r', strtotime('2012-09-10T10:00:00Z'));
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');