Solved

Converting dd/mm/yyyy string into a unix timestamp

Posted on 2008-10-28
3
2,308 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 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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