Solved

Converting dd/mm/yyyy string into a unix timestamp

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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 …

828 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