PHP date format /Date(1365004652303-0500)/
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');
how to convert jsonecode /Date(1366585200000+0100)/ back to readable date
You could use this function:
function convertToDate($d, $format = 'm-d-Y') {
// First argument should have format like '/Date(1363824000000+0000)/'
if (!preg_match("/[\d+-]+/", $d, $matches)) return null;
return gmdate($format, $matches[0]/1000);
}
Call it like this:
print convertToDate('/Date(1363824000000+0000)/', 'm-d-Y');
Output:
03-21-2013
How to convert this API Date format into PHP date?
This is a timestamp in milisecondes, i suggest the snippet bellow to extract the date:
$text = 'Date(1549170000000-0500)';
preg_match('#\((.*?)\)#', $text, $match);
$mil = $match[1];
$seconds = $mil / 1000;
echo date("d/m/Y H:i:s", $seconds);
c#: Get timezone offset stored in date object
Simply use DateTimeOffset
, not DateTime
- it's a similar structure, only with an explicit Offset
property. Plain DateTime
doesn't have a concept of time zones.
This means using it within MyClass
, and deserializing to it.
API returning invalid Date in PHP
Try this :
var isWcfJsonDate = /\/Date(.*)\//.test("Your Date String");
if (isWcfJsonDate)
{
dateStr = dateStr.match(/Date\((.*?)\)/)[1];
dateValue = new Date(parseInt(dateStr));
return dateValue;
}
dateValue = new Date(dateObject);
console.log( dateValue); //Use your date value
Convert datetime c# to unixstamp in php
PHP's [strtotime][1]
function is perfect for this. It can handle most common date formats, including strings.
Another option is with MySQL functions FROM_UNIXTIME and UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(datetime_column) FROM table
This usually is faster than a PHP function call.
Parsing JSON DateTime from Newtonsoft's JSON Serializer
Use one of the JsonConverters that come with Json.NET for working with dates to get a better format. JavaScriptDateTimeConverter will automatically give you a JavaScript date.
public class LogEntry
{
public string Details { get; set; }
public DateTime LogDate { get; set; }
}
[Test]
public void WriteJsonDates()
{
LogEntry entry = new LogEntry
{
LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
Details = "Application started."
};
string defaultJson = JsonConvert.SerializeObject(entry);
// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
// {"Details":"Application started.","LogDate":new Date(1234656000000)}
string isoJson = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
}
Documentation: Serializing Dates in JSON with Json.NET
Related Topics
PHP Function to Get Mp3 Duration
PHP Send Mail to Multiple Email Addresses
What Is the Most Efficient Way to Count All the Occurrences of a Specific Character in a PHP String
File_Get_Contents Behind a Proxy
Unique and Temporary File Names in PHP
Add a Prefix to Each Item of a PHP Array
Returning a Value in Constructor Function of a Class
Is It Wise to Use PHP for a Daemon
Close Open HTML Tags in a String
Laravel 5.3 Auth Check in Constructor Returning False
Retrieve All Hashtags from a Tweet in a PHP Function
How to Select Randomly with Doctrine
What's the Best Way to Pass a PHP Variable to JavaScript
How to Install the Ext-Curl Extension with PHP 7