bobthegoalie
bobthegoalie

Reputation: 211

Convert String (ISO) to Date PHP

I am trying to convert a string to a date in PHP. The string I receive is formatted:-

"2016-07-16T1:22:04.324+1030"

OR

"2016-12-20T03:24:59.000Z"

When I try this

$newDate = DateTime::createFromFormat("c", $exp);
$expirationDate = $newFormat->getTimestamp();

DateTime fails. Even when I try

$newDate = DateTime::createFromFormat(ISO8601, $exp;
$expirationDate = $newFormat->getTimestamp();

DateTime still fails.

Any suggestions are greatly appreciated.

Upvotes: 5

Views: 8345

Answers (4)

Ashish Kumar
Ashish Kumar

Reputation: 162

Though I am answering this question much later than the question was asked, I feel that following points will help substantiate the answers already provided.

Important points to keep in mind while using strtotime function:

1. Year is in two digit: If year is specified in the string in two digit, then values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. Possible difference may arise in 32 bit.

2. Use of Separator: Unexpected output may arise with the use of separator. The separator slash ("/") will indicate PHP that date is in American Format i.e. m/d/y. While the separator dash or dot ("-" or ".") will let PHP will assume European format i.e. d-m-y. Further, if the year is specified in two digit with a dash separator then PHP will assume it as y-m-d format.

3. Timestamp Range: The Typical valid timestamp ranges from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC which represent the minimum and maximum 32 bit signed integer. However for a 64 bit version of PHP, valid range of a timestamp is virtually infinite.

4. Checking if string is a valid timestamp: The string supplied as parameter in strtotime function can provide different result with different version. PHP earlier than 5.1.0 will return -1 on failure while later version will return false

Reference: http://php.net/manual/en/function.strtotime.php

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You can try like this:-

<?php

$time = "2016-07-16T1:22:04.324+1030";

echo date("d M, Y",strtotime(date($time)));

?>

Output:- 16 Jul, 2016 :- https://eval.in/594695

Reference taken:-

Converting ISO 8601 format to d M Y in PHP

Note:- you can change format of date according to your convenience. like D M d Y h:i:s O T . I checked this code on PHP 5.5.4 , PHP 5.5.14, PHP 7

Upvotes: 3

Naisa purushotham
Naisa purushotham

Reputation: 913

$dateval="2016-07-16T1:22:04.324+1030";
$dateval=str_replace("T"," T",$dateval);

$date = new DateTime($dateval);
echo $date->format('D M d Y h:i:s O T');//outputs your given time
echo "<br />";

or

echo date("D M d Y h:i:s O T",strtotime($dateval));//outputs your based on your timezone time

Hope it wll help

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

The date format you were passing over here is not the one that you were comparing within your function it should be like as

$exp = "2016-07-16T1:22:04.324+1030";
$newDate = DateTime::createFromFormat("Y-m-d\TG:i:s.uO",$exp);
echo $newDate->format("Y-m-d H:i:s O");//2016-07-16 01:22:04 +1030

Upvotes: 3

Related Questions