I have a xml file, containing several dates, in this format: 2016-07-23T07:00:00.000Z. I'm using a php function to convert this in to a format for publishing on a website. This should actually result in something like Saturday, 24th of July (24th, not 23rd, because of the time offset. My function somehow ignores the T07:00:00.000Z part and thus returns Friday, 23rd of July. Can anybody help me out with the proper way to convert this date?
Thanks, Peter
The string in question
2016-07-23T07:00:00.000Z
is a W3C datetime format (W3C DTF) (Complete date plus hours, minutes, seconds and a decimal fraction of a second) which can be properly parsed incl. the fractions of a second with the date_create_from_format](http://php.net/date_create_from_format) function:
$originalDate = "2016-07-23T07:00:00.000Z";
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
It does create a new DateTime which then can be formatted with the for PHP standard codes, e.g.
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->format('Y-m-d H:i:s'); # 2016-07-23 07:00:00
As that W3C format carries the timezone already and it is UTC, and you wrote you want a different one, you need to specify it:
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->setTimezone(new DateTimeZone('Asia/Tokyo'))
->format('Y-m-d H:i:s');
The reason why this is not visible (and controlable with the code given) in the previous answer is because date formats according to the default set timezone in PHP where as each DateTime has it's individual timezone.
An equivalent with correct parsing (incl. decimal fraction of a second) with the other answers then is:
$dateTime = date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
date('Y-m-d H:i:s', $dateTime->getTimestamp());
Hope this explains it a bit better in case you need the complete date value and / or more control on the timezone.
For the format, see as well: In what format is this date string?
$oldDateTime= "2016-07-23T07:00:00.000Z"; // Your datetime as string, add as variable or whatever.
$newDateTime= date("Y-m-d H:i:s", strtotime($originalDate));
Related
I have a xml file, containing several dates, in this format: 2016-07-23T07:00:00.000Z. I'm using a php function to convert this in to a format for publishing on a website. This should actually result in something like Saturday, 24th of July (24th, not 23rd, because of the time offset. My function somehow ignores the T07:00:00.000Z part and thus returns Friday, 23rd of July. Can anybody help me out with the proper way to convert this date?
Thanks, Peter
The string in question
2016-07-23T07:00:00.000Z
is a W3C datetime format (W3C DTF) (Complete date plus hours, minutes, seconds and a decimal fraction of a second) which can be properly parsed incl. the fractions of a second with the date_create_from_format](http://php.net/date_create_from_format) function:
$originalDate = "2016-07-23T07:00:00.000Z";
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
It does create a new DateTime which then can be formatted with the for PHP standard codes, e.g.
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->format('Y-m-d H:i:s'); # 2016-07-23 07:00:00
As that W3C format carries the timezone already and it is UTC, and you wrote you want a different one, you need to specify it:
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->setTimezone(new DateTimeZone('Asia/Tokyo'))
->format('Y-m-d H:i:s');
The reason why this is not visible (and controlable with the code given) in the previous answer is because date formats according to the default set timezone in PHP where as each DateTime has it's individual timezone.
An equivalent with correct parsing (incl. decimal fraction of a second) with the other answers then is:
$dateTime = date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
date('Y-m-d H:i:s', $dateTime->getTimestamp());
Hope this explains it a bit better in case you need the complete date value and / or more control on the timezone.
For the format, see as well: In what format is this date string?
$oldDateTime= "2016-07-23T07:00:00.000Z"; // Your datetime as string, add as variable or whatever.
$newDateTime= date("Y-m-d H:i:s", strtotime($originalDate));
I am trying to convert a date to a timestamp at the exact midnight point.
To do this, I am using the following little function.
function converttotimestamp($date)
{
$date = str_replace('/', '-', $date);
$date = $date.' 00:00:00';
$date = DateTime::createFromFormat('m-d-Y H:i:s',$date);
return $date->getTimestamp();
}
So as you can see, I am attaching a midnight time at the end.
I tried using this as shown below
echo converttotimestamp('7/22/2014');
So as you would expect when you run this in a unix converter, you would get 1405987200.
But In my case it returns 1405976400 whicj translates to Mon, 21 Jul 2014 21:00:00.
Oh. I am in Kenya.
The reason you may be seeing a different time returned than the one you were expecting is likely because you haven't considered the relevant timezones. There are a couple different ways you can set the timezone. You can set it during runtime:
http://php.net/manual/en/function.date-default-timezone-set.php
You can set it in your PHP config file:
http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
Or you can set the timezone of your DateTime object:
http://php.net/manual/en/datetime.settimezone.php
Whenever you are converting between dates, you must consider the relevant timezone, as this is the only way for the system to determine how to switch between date formats, make comparisions and output specific dates and times. For example, if you want to convert a date and time to a timestamp, the system must know the timezone of the input date and time so it can convert properly. Take a look at strtotime:
http://us2.php.net/manual/en/function.strtotime.php
Unix timestamps are GMT timezone, so make sure you convert your datetimes accordingly. HTH.
I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime
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');
Not sure whats up with the code
$date = strtotime("%b %d, %Y", $datedata);
$time = strtotime("%I:%M:%S %p", $datedata);
The time i gets from the DB is 1298747601 and is the $datedata
I have date_default_timezone_set('America/Los_Angeles'); at the top of the script.
Use strftime() or date(), not strtotime(). Strtotime is meant to format textual dates, not timestamps.
It's better to use the DATETIME datatype in your database instead of an int containing a timestamp. Using a DATETIME means easier calculating with dates and you can format the date directly in your query.
strtotime() is for converting a string to a time like, $timestamp = strtotime("1:33 PM EST Next Friday");.
You want date(), which takes a format string and a timestamp to create a formatted date time string, and uses the current time (from time()) if no timestamp is passed, like date("h:i:s A T, M jS, Y", $timestamp) which would output something like "1:33:00 PM EST, March 4th, 2011". Also, note that if you want to do words in the format, like "23rd of January", and the letters in the words are also date formatting characters, in this case the 'o' in 'of' is the ISO-8601 year number formatting option, you must escape it with a \ slash like "jS \of F". As the f is not a formatting option, it does not need to be escaped, but if it did, then it would be "\o\f"
There's also a DateTime object that's built into more recent PHP versions that you can look into.
I disagree with Ray that it is better to store a DateTime datatype in SQL rather than an integer timestamp. They're about the same. You can still search for date ranges in the DB simply by finding numbers greater than X timestamp and less than Y timestamp, and, as a plus, everything that you get from the DB is already in a timestamp format that can be used easily by PHP without having to convert it to a proper timestamp.