Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date format /Date(1365004652303-0500)/

Tags:

I am calling an API from where I am getting date /Date(1365004652303-0500)/, I don't understand what format this is. How is this date format called? I was not sure what to google for such type of format.

Can anyone help me out in getting this date in Y-m-d H:i:s format?

The API I am calling is on .NET server. And when I call it using PHP's file_get_contents and json_decode it gives me the following Date format for created date: /Date(1365004652303-0500)/

like image 863
Kalpesh Avatar asked May 25 '13 12:05

Kalpesh


People also ask

How can I get current date in YYYY-MM-DD format in PHP?

$date = date("yyyy-mm-dd", strtotime(now));

What is T and Z in dateTime?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).

How convert date from yyyy-mm-dd to dd-mm-yyyy format in PHP?

Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.


1 Answers

First you need to understand the format you have

/Date(1365004652303-0500)/ 

Then you have

  • time stamp (U) = 1365004652
  • Milliseconds (u) = 303
  • Difference to Greenwich time (GMT) (O) = -0500

Build a Format

$date = '/Date(1365004652303-0500)/'; preg_match('/(\d{10})(\d{3})([\+\-]\d{4})/', $date, $matches); $dt = DateTime::createFromFormat("U.u.O",vsprintf('%2$s.%3$s.%4$s', $matches)); echo $dt->format('r'); 

Output

Wed, 03 Apr 2013 15:57:32 -0500                             ^                             |= Can you see the GMT ?  

interface DateFormatParser {     /**      * @param $string      *      * @return DateTime      */     public function parse($string);  }  abstract class PregDateParser implements DateFormatParser {     protected $pattern, $format, $mask;      public function parse($string) {         $string = (string)$string;          $pattern = $this->pattern;         $format  = $this->format;         $mask    = $this->mask;          $r = preg_match($pattern, $string, $matches);         if (!$r) {             throw new UnexpectedValueException('Preg Regex Pattern failed.');         }         $buffer = vsprintf($mask, $matches);         $result = DateTime::createFromFormat($format, $buffer);         if (!$result) {             throw new UnexpectedValueException(sprintf('Failed To Create from Format "%s" for "%s".', $format, $buffer));         }         return $result;     } }  class JsonTimestampWithOffsetParser extends PregDateParser {     protected $pattern = '/^\/Date\((\d{10})(\d{3})([+-]\d{4})\)\/$/';     protected $format  = 'U.u.O';     protected $mask    = '%2$s.%3$s.%4$s'; }  $date   = '/Date(1365004652303-0500)/'; $parser = new JsonTimestampWithOffsetParser; $dt     = $parser->parse($date);  echo $dt->format('r'); 
like image 69
Baba Avatar answered Sep 28 '22 13:09

Baba