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
Related
Currently working with a company that sends info via XML which is read and inserted into our database and they've asked how they want us to send dates through, string, integer timestamp etc and it peaked my curiosity a little for me to google but not able to find the answer to the following question.
If a string for a date is sent as 03/01/2016 how does php when convert into a timestamp using strtotime() determine the format?
UK its read as 3rd January 2016 but to the US its 1st March 2016 so how does php know to convert it to the correct region?
Does it use timezone set in the php.ini? does something have to be declared beforehand.
Never really though about it and curious to know as it would probably make my dealings a lot easier in future if information such as a date is given to me in that format
strtotime() returns timestamp and it only parses date in specific format like "m/d/y" or "d-m-y" or "d.m.y". It is clearly state on documentation that:
Each parameter of this function uses the default time zone unless a
time zone is specified in that parameter.
And regarding format accepted:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at
the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y format
is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD)
dates or DateTime::createFromFormat() when possible.
Hope it helps...
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 a script which is fed dates in numerous different formats.
I want to save these dates as timestamps so they can easily be manipulated/ordered.
When i try an convert a mm-dd-yyyy type date to a timestamp, it fails.
When the script runs, it does not know what format it will be fed, and as such this cannot be specified. Near all other formats of date seem to be converted fine.
Could anyone advise how to fix this, or alternatively an alternative way that all date formats can be converted to an orderable, consistent format that can be manipulated?
Many Thanks
It sees strings with - in them as dd-mm-yyyy and / as mm/dd/yyyy.
See also this question and the comments on the documentation.
Possible solutions / workarounds:
on php 5.3, use date_create_from_format
on older php and not on windows, use strptime
if neither can be used, either replace the - to / when necessary, or use one of the regexes suggested you can find through the linked question.
Note however that at some time you do need to know what the format is to start with. Computers are not mindreaders. They can't, and never will be able to, distinguish between mm-dd-yyyy and dd-mm-yyyy in the overlap ranges (both mm and dd <= 12) if you don't provide the distinction.
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.
Hey, long story short I have inherited some terrible code. As a result a string comparison is buggy when comparing dates due to the format of the date. I am trying to convert the date to a valid DateFormat syntax so I can run a proper comparison.
These are some samples of the current format:
12/01/10 at 8:00PM
12/31/10 at 12:00PM
12/10/09 at 5:00AM
and so forth. I'd like to convert this to a YYYYMMDDHHMM format i.e 201012012000 for comparison purposes. If anyone can give me a quick regex snippet to do this that'd be appreciated as right now i'm hitting a brick wall for a regex. I can do it by exploding the string over several times etc but I'd rather do it in a more efficient manner.
Thanks!
Working with dates in strange formats is very easy with the DateTime class which was built into PHP 5.3.
No need for regex or anything fancy:
$date = DateTime::createFromFormat('m/d/y \a\t g:iA', '12/10/09 at 5:00AM');
print_r($date);
Once it is a date object you can have it in any format you want.