Solved

Converting dd/mm/yyyy string into a unix timestamp

Posted on 2008-10-28
3
2,319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
Cornelia Yoder 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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

736 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