Convert multiple dates to Unix time in PHP - php

Hi I have a few dates coming in random formats:
Wed, 16 Mar 2011 15:27:48 +0000
or
2011-03-16T17:42:05+0000
and I need to be able to convert them into Unix time in PHP. I assume I need to use mktime() , ,but how do I format my mktime ? I'm having trouble understanding the examples in the manual :/
Thanks

strtotime() can work, but if the dates you're passing in are ambiguous (what is 01/02/03? Feb 1st, '03? Mar 2 '01?) it'll turn around and chew you a new one.
Safest option is to use date_create_from_format(), which lets you specify an explicit format for the input.

strtotime

Related

How to compare between different date formats?

I have four different date formats that I will store in a DB, Then show the latest ones.
The four different formats:
$a = '27. júní 2018 04:53';
$b = 'Friday, 09 March 2018';
$c = 'Fri, 29 Jun 2018 11:00:00 GMT';
$d = 'Mon, 18 Jun 2018 06:52:20 +0000';
They will be stored in a MYSQL Database.
What should I do with them?
Can SQL or MYSQL date type do the work?
Should I convert them using strtotime()?
Should I extract some data from specific ones to male them match?
MySQL has three data types suitable for date/time values. These are pretty well explained in the documentation.
You have three types of values:
Date alone.
Date with a time, but no time zone.
Date with a time and a time zone.
But the basic questions to ask:
Do you need just the date or is the time also necessary?
Do you need the time zone?
If you need just the date, then date will do. Otherwise, you want datetime or timetamp. Which depends on how you want to treat timezones.
If you actually need to store the timezone with the value, then I would suggest that you think hard about whether this is really necessary. Typically, timestamp is sufficient, because it stores values in UTC which can then be converted to any original timestamp. You can store the original time zone in an alternative column.
If you really need to handle all three types with no compromise, then you might need to use a string. In this case, you should use a correctly formatted string:
YYYY-MM-DD
YYYY-MM-DD HH:MI:SS
YYYY-MM-DD HH:MI:SS+TZ
The first two are readily converted to the appropriate type in MySQL. For the third, you can extract the timezone and add it as an offset (with a bit of work).
However, I doubt whether this approach is necessary. A datetime is probably sufficient.

Add 48 Hours to MySQL Datetime using PHP and print it in Javascript Date format

I have a datetime stored into database like this
2013-02-22 00:00:00
What I want to do is using PHP, it should count this date + 48 Hours and then print a message like this
Sat Feb 23 2013 09:38:47 GMT+0530 (India Standard Time)
Which is actually a default return value javascript Date() function. So that I can pass it to the countdown timer plugin I am using.
Any suggestion how can I achieve that?
Thanks in advance.
You could also try:
$dayAfterTomrrow = strtotime("2013-02-22 00:00:00")+172800;
echo date("r", $dayAfterTomorrow);
.. which will give you a format you can pass to a javascript date object.
But you don't even have to pass in a formatted date to the "new Date()" instantiator in javascript - you can also pass in a timestamp (unix epoch) in milliseconds. So the second line of my example isn't even necessary if you just take the stamp of $dayAfterTomorrow and multiply it by 1000 in javascript.
It is possible to use date() and mktime() together to find dates in the future or the past.

Correct input values of strtotime

I want to convert a date by the following statement
$date=date('d M Y, H:m',strtotime($date));
It printed out 09 Sep 2012, 11:09 when I tried various values of $date:
11:00 AM Sunday, 09 Sep 2012
Sunday 09 Sep 2012, 11:00 AM
09 Sep 2012 11:00 AM
What date format does the strtotime() function need? It seems to me at least one of them meets the "English textual datetime description" condition.
Valid formats for strtotime are detailed here.
From this site (http://www.tuxradar.com/practicalphp/4/5/2)
Be wary of dates such as this one: August 25, 2003, 10:26am. Although this may look perfectly well-formed, strtotime() is not able to handle it because it has commas in there - yes, they make it much more readable for us, but strtotime() gets confused handling them. If you have dates with commas in, be sure to strip them out using str_replace().
How to deal with strtotime() -> http://www.w3schools.com/php/func_date_strtotime.asp
Looks like the only way to go is to sort the elements in proper order by regular expressions and then replace the month name by corresponding number. Such a string strtotime will manage.
Note: I'm talking about PHP 5.2x
EDIT: I found the reason, why strtotime "wasn't working" was that I made a typo in the date formating string, there should be 'd M Y, H:i'

Convert a time string eg "Wed, 24 Nov 2010 13:10:00 EST" to DataTime in PHP

I am parsing a XML , and I get the created time as: Wed, 24 Nov 2010 13:10:00 EST
My requirement is to convert this time into the specified time format using DateTime function before inserting it in the database.
Can anyone help me with this.
Thanks you
Zeeshan
Check out the duplicate link I pointed out above.
The r date format might work for you - not sure because of the time zone name. If in doubt, build the mask yourself, using e as a placeholder for the time zone identifier.
The PHP manual on date has the list of placeholders.
Use date_create_from_format function to get DateTime. Your format looks like "D, d M y H:i:s T".

How to Convert to UTC

I'm getting dates & times from various sources (e.g. file's date/time from FTP server, email's date/tiem received, etc.) and need to store them all as UTC (so they all have a common reference). How do I do this? What pieces of information do I need in order to properly do the conversion.
This is for a PHP web application. So, I can get my server's time zone. I'm not sure what to do next. Here are some sample inputs:
Mon, 28 Jun 2010 12:39:52 +1200
2010-06-25 15:33:00
For the first case the offset is there so it should be trivial, the second example however will be considered as UTC (or any other default timezone). This is what I suggest:
date_default_timezone_set('UTC'); // set default timezone
$one = strtotime('Mon, 28 Jun 2010 12:39:52 +1200');
$two = strtotime('2010-06-25 15:33:00'); // Already UTC? Must be...
$one and $two will hold the timestamps of the correspondent time converted to the UTC timezone.
You can use strtotime() to convert whatever time format you have into a timestamp and then use whatever function, probably date(), to put it in the format you want everything to be stored in.

Categories