2010年8月10日 星期二

[PHP] convert date to timestamp 時間戳記

http://php.net/manual/en/function.strtotime.php
strtotime  Parse about any English textual datetime description into a Unix timestamp


Datetime to Timestamp

$timestamp = strtotime($datetime);


Timestamp to Datetime

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


<?php 
    // Calculate the difference in days.  
    $date1 = strtotime(2010-10-10);
    $date2 = strtotime(2011-12-01);
    $daysDiff = ($date2 - $date1)/(24 * 60 * 60);

    // Which is the latest?
    if ($date2 > $date1) {
      echo "$date2 is later than $date1";
    }
?>

可以用以上Code來作時間前後的比較, But if you do, your code will be susceptible to the 2038 bug.
Related Posts Plugin for WordPress, Blogger...