CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005
    Posts
    73

    convert dates in Unix time format

    Hello all,
    I have around 900 dates in the format of DD-MM-YYYY and I want to convert them in Unix time format...
    Is there a way to do this? I was thinking of passing them into an array, and then, for each date, I would use the function date() I suppose and get the Unix time format of them... But I don't know the way to do it...

    So, for instance, if you have:
    $date_to_format='05-10-2006' , how would I get the Unix timestamp?

    Thank you in advance!

  2. #2
    Join Date
    Dec 2004
    Posts
    438

    Re: convert dates in Unix time format

    You want to use either mktime or strtotime. Make time basically has a hour, minute, second, month, day year format. You'd have to split your date up using explode for example, and then pass in the parameters:

    $dateArray=explode('-,$mydate);
    $myTimestamp=mktime(0,0,0,$dateArray[1],$dateArray[0],$dateArray[2]);

    http://us2.php.net/explode
    http://us2.php.net/manual/en/function.mktime.php

    Alternatively you could just see whether strtotime will just understand your date format and produce the timestamp:

    http://us2.php.net/strtotime

    Strtotime will produce a timestamp from almost anything, for example, strtotime("Last thursday"); but using different formats generates sometimes incorrect results. Always check that your timestamp has worked by then passing it into date:

    date('d-m-Y',$myTimestamp);

    Hope that helps.
    Last edited by Nibinaear; February 11th, 2007 at 06:53 AM.

  3. #3
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    Talking Re: convert dates in Unix time format

    If you are getting your dates from mysql database you can also try unixtimestamp(), a mysql's built function.



    SELECT UNIXTIMESTAMP(d_date) as t_tmestamp FROm table
    All consequences are eternal in some way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured