Solved

Converting dd/mm/yyyy string into a unix timestamp

Posted on 2008-10-28
3
2,298 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

10 Experts available now in Live!

Get 1:1 Help Now