PHP auto correct date in specific format - php

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.

Related

Getting a future unix timestamp using a specific date and time?

I run a business and have been developing a CMS / invoicing on my website.
"Appointment"
12:30 PM Thu Aug 5 2021
Though I can format this any way I like, is it possible at all to get the unix time stamp of that?
The purpose of doing so would be so I can just insert the unix timestamp and be able to sort my appointments by the closest to date.
Thank you.
$timestamp = strtotime('1/1/2021 21:00');
Replace 1/1/2021 21:00 to the desired date.

PHP strtotime with output from Bootstrap DatePicker

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

Difference in date value in localhost

In my localhost WAMP stack, I am having a database table which has a column "Birth-date" as a "date-time" column type.
I am running the below SQL for that table to get the birth-date of the current year and its unix timestamp value. No issues here.
SELECT dateValue,
DATE_ADD(dateValue, INTERVAL YEAR(CURDATE())-YEAR(dateValue) YEAR) "Date1",
UNIX_TIMESTAMP(
DATE_ADD(dateValue, INTERVAL YEAR(CURDATE())-YEAR(dateValue) YEAR)
) "Date2"
FROM base_date
For example I get the below output from the SQL.
DATEVALUE DATE1 DATE2
-------------------------------+-------------------------------+-----------------
December, 18 1980 00:00:00+0000 December, 18 2013 00:00:00+0000 1387305000
December, 20 1985 00:00:00+0000 December, 20 2013 00:00:00+0000 1387477800
The problem is that when I try to display that unix formatted timestamp value in PHP, the date is not shown correct. There is a change of 1 day.
echo gmdate("Y-m-d", "1387305000"); // Displays 2013-12-17 and not 2013-12-18
echo gmdate("Y-m-d", "1387477800"); // Displays 2013-12-19 and not 2013-12-20
The confusing part is that :
When I test this with SQL Fiddle values, it worked correct. The unix timestamp out of the SQL is different for the same date in my localhost mysql(5.5.24) and the version available in sqlfiddle.com(5.5.32).
Fiddle: http://sqlfiddle.com/#!2/25e032/3/0
What could be the difference? Is it related to timezone or sql version or something else?
My host has the timezone set on MySQL to their local timezone (Pacific Time), so a PHP time() value (GMT) put into the database and displayed as a MySQL time is off by 8 hours or so. So, it may be a matter of your host's MySQL configuration. Be careful about mixing PHP timestamps and MySQL timestamps.
You are using gmdate() which adjusts to GM timezone. What is the default timezone set for your PHP? Try changing gmdate() to date() and see if it works correctly.
date_default_timezone_get() -> http://www.php.net/manual/en/function.date-default-timezone-get.php
On the gmdate() documentation you can get an explanation, look at Example 1:
http://www.php.net/manual/en/function.gmdate.php

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'

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