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'
Related
In my code users post date in content. Which is may be in different formats the format which is used in majority auto sets in MySQL date time field format i.e. yyyy-mm-dd hh:mm:ss. My problem is most of them have typo mistakes or different formats which my code doesn't pick correctly and it returns date something like this 1970-01-01 05:00:00. I am in deep from this issue. Is there any function that auto corrects the date time even if there is a typo mistake in it and if time is not available it auto adds the time to it?
Here are some examples of different formats I get
30 September 2017 | 09 31 AM
29 September 2017 | 02:30 PM
27/07/2016 | 08:20 PM
19/09/2017| 01:32 PM
14-July-2017 03:31 PM
September 5 2017
April 7 2016 04:55 PM
Here is my current PHP code
$get_date = ""; //Date in text form
$show_dated = strtotime(str_replace('|', '', $get_date);
$get_date = date("Y-m-d H:i:s", $show_dated);
echo $get_date;
Try using date_parse() instead of strtotime(). I did this for a while, and it functioned better. Ultimately my solution was to build a custom parser based on the confused mess of user inputs. I eyeballed 2000 entries to develop a 'gold standard' set of results, and then fine-tuned an algorithm to match until it performed 100% correctly.
Bootstrap time picker gives me Date in this format:
Mon Mar 16 2015 00:00:00 GMT-0500 (Central Daylight Time)
I would like to use it as a Date object in PHP.
I am attempting to use it like:
date("Y-m-d H:i:s" , strtotime("Mon Mar 16 2015 00:00:00 GMT-0500 (Central Daylight Time)"));
But that results in:
1969-12-31 18:00:00
Which I understand to be pretty close to EPOCH.
I'm thinking I'm either using the wrong function, or I formatted date() wrong.
I am using the TimePicker from here.
strtotime only works on certain formats of which can be found here http://php.net/manual/en/datetime.formats.php
Mar 16 2015 00:00:00 GMT-0500
The above portion should return the right time value for you. Either change the way that the picker is outputting the date, without knowing which picker I will assume its this one http://bootstrap-datepicker.readthedocs.org/en/latest/options.html#format or do some string manipulation on the current date format from the picker (the first option would be simpler and easier to read).
First off, this is not a question about how to fix a problem because my date is outputting 1969.
This is a question about why time does not exist before 1970 or after 2038 when using date().
I've tried seaching SO and Google but all that turns up is people getting errors when using date() incorrectly, resulting in the familiar output of December 31, 1969 5:00 pm
Does anyone know why it can't go before 1970? Should we stop using date() since it will be unusable after 2038? What's the history on this? What's the work around for working with dates outside of this range?
It's 2038 problem
and look, I'm in year 1653
This is explained on the PHP manual page for date():
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer).
The fact that you're getting December 31, 1969 means that you're likely supplying an odd timestamp parameter to date(), resulting in a date that isn't what you expect. Like #Mitch Wheat said in his comment, this relates to Unix time since it is relative from January 1, 1970.
Compare the number 2^31 and the number of seconds between the two date.
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
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".