PHP strtotime with output from Bootstrap DatePicker - php

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).

Related

PHP auto correct date in specific format

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.

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 multiple dates to Unix time in 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

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".

Cocoa Core Data to PHP: Converting date

I have dates that are being stored in a database by core data. I then am using php to print out this date information but the date is coming out wrong.
When I store Aug 2, 2009 in core data it comes out in the php as Fri, August 4, 1978. How do I fix the conversion?
I'm guessing a bit here, but the limited evidence fits the hypothesis...
NSDate has an absolute reference date of 1 Jan 2001 (GMT).
PHP time() uses the Unix Epoch date of 1 Jan 1970 (GMT).
It looks like you have an offset of 31 years - or rather 978307200 seconds.
(NSTimeInterval) delta = [[NSDate dateWithTimeIntervalSinceReferenceDate:0] timeIntervalSince1970];
Solution would be to either create your dates in Cocoa with the reference date of 1970, or to add/subtract the offset in Cocoa or PHP.
James

Categories