Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    96
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Exclamation Daft date question

    Hi,

    I know this is probably a stupid question but I can't get my head around the date() and strtotime() functions in PHP.

    How can I get the following date format into a Unix timestamp?

    I.e. "18/07/2009 11:44:00" into a Unix timestamp? I've tried various combos and just can't get it to work!!

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Dates using a slash / as the separator must be in the U.S. mm/dd/yyyy format (sorry to all the countries that use dd/mm/yyyy) - http://www.gnu.org/software/tar/manu...ms.html#SEC116

    You either need to use a different separator or break the date into its' separate pieces and put it back together or use the mktime() function.

    Is there a good reason why you want to use a Unix Timestamp, given all the problems with using them for most purposes?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    echo strtotime(str_replace('/','-','18/07/2009 11:44:00'));
    echo 
    '<br/>'strtotime('2009-07-18 11:44:00'); 
    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Funny that this topic just came up in another post...

    Put this function in your script:
    PHP Code:
    function DateToTimeStamp($date){
    // Convert a date in this format: "18/07/2009 11:44:00"
    // To a UNIX TimeString (eg. 1255624200)

    // Separate the date from the time.
    $dt=explode(" ",$date);

    // Separate the month, day, year.
    $date=explode("/",$dt[0]);

    // Separate the Hour from Minute
    $time=explode(":",$dt[1]);

    // Generate a UNIX TimeStamp
    $new_time=@mktime($time[0],$time[1],$time[2],$date[1],$date[0],$date[2]);
    return(
    $new_time);


    And call it like this ...

    $textdate = "18/07/2009 11:44:00";
    $unixtime = DateToTimeStamp($textdate);
    echo $unixtime;


  •  

    Posting Permissions

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