Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mktime issue

Status
Not open for further replies.

pease

Programmer
Apr 2, 2009
29
0
0
GB
Morning all,

I have a table containing 4k+ records each has a date stored as a text field in the format d/m/yy (ie 01/01/99 or could be 1/1/99).

Ive ran a script against the table to populate the UNIX date for each record using a straight forward logic of splitting the date into its day/month and year values then using mktime.

The script has worked for almost all of the dates however there are a range at the start which have all been populated as 0 (zero).

These are dates in the range 07/04/1892 to 07/12/1901 (the dates from 14/12/1901 to the present day are then correctly populated).

I assumed it was a mktime problem with the older dates however Ive tested them on a basic online converter and they convert ok there.

Im a little lost on this one, Ive echo'd the variables and the script is correctly splitting the data and producing valid values for day, month and year.

Any thoughts would be appreciated.
 
Hi

If that "table" means an SQL database's table, then you should store the dates in a field of appropriate type.

Try to use the [tt]DateTime[/tt] class instead. That seems to handle such date correctly :
PHP:
$d=DateTime::createFromFormat('d/m/Y','07/04/1892');
echo $d->format('Y-m-d');
Code:
1892-04-07

Feherke.
 
Thanks for that,

Maybe its just a personal thing but I always prefer to store dates as the Unix date rather than a date format and then format it accordingly on output.

Im lost as to how php isnt calculating these as it is the others?
 
the dates you are working with may be outside the limits for your php installation (32bit floating numbers).

if you are working solely with days (as opposed to times), you might find julian dates easier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top