I'm consuming an API (XML output) with PHP and it is returning all timestamps in the following format. Believe it or not this isn't in the docs anywhere.
Is this a standard date format?
What format is this timestamp in?
How can I convert it to a unix timestamp in PHP?
/Date(-62135575200000-0600)/
It's the default JSON format of date.
Looks like a .NET Date string, which would mean that it is milliseconds since 1970, and the timezone.
Related
This question already has answers here:
How to convert ISO8601 to Date format in php
(6 answers)
Closed 2 years ago.
I'm currently having trouble figuring out how to convert the current timestamp to one that works as a parameter for google calendar. I'm using PHP.
Right now the format is YYYY-MM-DDTHH:mm. The date I'm trying to convert is:
2020-03-11 10:00:00
So far I've tried using gmdate and passing in the current date with strtotime but I can't figure out how to make it the right format.
Thanks for any help.
you can do in php like:
date('your needed format here', strtotime($yourtime))
check https://www.php.net/manual/en/function.date.php for format detail. that will give you the needed string.
date() will convert unix time format in given format, the strtotime() makes unix time out of almost any time format string, it will detect the format automatically.
How can I get 2017-08-29T09:43:42.335Z date format in PHP.
And what does T09:43:42.335Z correspond to?
T09:43:42.335Z is the time (including milliseconds) in the Zulu timezone (the T is just a separator).
This is not a time format that's built-in in PHP. As of PHP 7.0.0 you could make it yourself like so:
$date->format('Y-m-d\TH:i:s.v\Z');
In earlier PHP versions there is no "milliseconds" option, so you would have to concatenate that yourself.
The Zulu timezone is equal to UTC+0. Make sure you convert your time to UTC+0 before you print this out, the format command won't do that for you.
While using google api for feed, I get blog post's creation time in format - 2015-04-11T06:33:00.001-07:00 which is datestamp for another timezone. I want to convert datestamp to IST timezone (eg. 2015-04-12T09:51:00.001+05:30) with php. How do I do that?
In another question what all I could find is convert 2015-04-12T09:51:00.001 to another format, that's it. I am unable to convert it to mentioned format.
This can be easily accomplished with DateTime:
$dt = new DateTime('2015-04-11T06:33:00.001-07:00');
$dt->setTimeZone(new DateTimeZone('Asia/Colombo'));
echo $dt->format('c');
demo
Does this date format have any particular name and/or is it used in any particular programming language?
2012-11-28T12:52:22+0000
I need to convert this into JavaScript or PHP date, possibly without using string concatenation.
Kind of what somebody did here: Convert a Unix timestamp to time in JavaScript
Does any of these 2 programming languages have any pre-made function to do so?
PHP handles this easily with the DateTime class:
$dt = new DateTime('2012-11-28T12:52:22+0000');
echo $dt->format('Y-m-d');
See it in action
format() accepts the same parameters as date() so you can format it into any format you need.
Reference
DateTime
date()
That's an ISO-8601 date, which is becoming very common on the web.
It is an interoperability format, supported by many frameworks, and was added to PHP in version 5.1.0.
In JavaScript, it is supported by the Date.toISOString() method, and libraries such as Moment.js - among others.
You can construct a JavaScript Date from an ISO string using its constructor:
var date = new Date('2012-11-28T12:52:22+0000');
Be aware that JavaScript is notoriously bad with dates. It only understands dates from a local perspective, or from UTC. Any offset you provide will be merged into the date. That is being worked on in Moment.js. see here.
Yes, it's called an ISO-8601 date. With PHP you generate it with date('c') and with JavaScript:
var date = new Date('2012-11-28T12:52:22+0000');
date.toISOString();
As you can see, the argument to Date() can be just such a string.
I have different DateTime formats and need to convert them to a standardized format.
Is there any build in PHP method which accepts different formats and returning them as a DateTime object or something else easy to format? I searched php.net and also this forum but can't find anything.
Just want to save time before I start coding something which probably already exists.
my formats to catch:
d.m.Y
d/m/Y
d-m-Y
m.d.Y
m/d/Y
m-d-Y
Y.d.m
Y/d/m
Y-d-m
Y.m.d
Y/m/d
Y-m-d
If there is no PHP method I will write my own class or if someone has something handy to share... :)
Thanks!
You can use DateTime::createFromFormat and your formats in a loop and check if valid object has been created