Solved

Converting dd/mm/yyyy string into a unix timestamp

Posted on 2008-10-28
3
2,304 Views
Last Modified: 2013-12-13
Hi,

I have dates stored in my database as strings in the format:

dd/mm/yyyy

and i want to convert them to a unix timestamp when I pull them out of the database so that I can sort them by date.

(when i use sort on the original string it seems to treat it as a nuber e.g. 20/05/1985 as 20051985)

Thanks
0
Comment
Question by:DrZork101
3 Comments
 
LVL 27

Accepted Solution

by:
yodercm earned 500 total points
ID: 22820741
The best thing to do would be to convert your database to use standard date format.  Then you could sort using the query.

If that is impossible, then you can retrieve the dates into standard format using

SELECT CONCAT(SUBSTRING(mydatefield,7,4),'-',SUBSTRING(mydatefield,4,2),'-',SUBSTRING(mydatefield,1,2)) FROM mytable

If you must retrieve them in the dd/mm/yyyy format, then you can use php to convert by
$year = substr($olddate,6,4);
$month = substr($olddate,3,2);
$day = substr($olddate,0,2);

and then use mktime() to convert to a unix timestamp by

$timestamp= mktime(0,0,0,$month,$day,$year);

http://us3.php.net/manual/en/function.mktime.php

http://us3.php.net/manual/en/function.mktime.php


0
 
LVL 6

Expert Comment

by:Chorch
ID: 22820778
Hello,

this should work:

$date = '20/05/1985';
list($d, $m, $y) = explode('/', $date);
echo mktime(0, 0, 0, $m, $d, $y);

Anyway, consider storing dates as 'date' in mysql table so that you can order directly in query.

Regards
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22821116
In data base storage, you should use a DATE or DATETIME data type.  These use the ISO 8601 date format which looks (more or less) like YYYY-MM-DD HH:MM:SS.

The ISO format lets you sort the dates.  It also lets you use strtotime() to get UNIX timestamps from the dates or parts of the dates without the overhead of mktime().  This means it is easy to do date-related calculations.

Using a combination of date() and strtotime() you will find that your life gets much easier fast!  So I vote with YoderCM on the approach - refactor the database to incorporate the correct data type.  More information is available here:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

HTH, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now