开发者

should I use 'strtotime' here?

I have a website where I display information from a mysql table such as date of last change. In mysql, the field typ开发者_开发知识库e is 'TIMESTAMP' ... I have a code like this:

$postdate = date( "j F", strtotime( $row['insert_date'] ) ); // Getting the date from the database
$posthour = date( "H:i", strtotime( $row['insert_date'] ) ); // Getting the time from the database
    if ($postdate == date("j F")) {$postdate = "Today";}
    if ($postdate == date('j F', strtotime('-1 day'))) {$postdate = "Yesterday";}
    if ($postdate == date('j F', strtotime('-2 day'))) {$postdate = "Day before yesterday";}

I have heard that strtotime is a slow function. Does anyone have any suggestions on how to rewrite this to improve performance in any way?

NOTE: the TIMESTAMP in mysql table may be changed if you suggest it, thats no problem!

Thanks


I would go for something like this (using DATEDIFF):

SELECT ..., DATEDIFF( `insert_date` , CURRENT_TIMESTAMP ) AS diff_insert, ...
FROM xxxxxx ...

then in php:

if ($row['diff_insert'] == 0) {
  $postdate = "Today";
} else if ($row['diff_insert'] == -1) {
  $postdate = "Yesterday";
} else if ($row['diff_insert'] == -2) {
  $postdate = "Day before yesterday";
}


Like the above post stated you can use DATEDIFF in MySQL, but since PHP 5.3 you can also use the 'diff' method on a DateTime class.

Example:

<?php 
 $dateObject = new DateTime(); // No arguments means 'now'
 $otherDateObject = new DateTime('2008-08-14 03:14:15');
 $diffObject = $dateObject->diff($otherDateObject)); 
 echo "Days of difference: ". $diffObject->days; 
?>

The 'DateTime' diff function returns a DateInterval object. This object consists of variabeles related to the difference. You can query the days, hours, minutes, seconds just like in the example above.

See the manual about it: http://nl2.php.net/manual/en/datetime.diff.php . Sadly, it's a PHP 5.3> only feature.


Modify your SELECT statement to use UNIX_TIMESTAMP(), this will be more efficient as strtotime . e.g.:

SELECT ... UNIX_TIMESTAMP(`insert_date`) AS `insert_datestamp`, ...

Then:

$posthour = date( 'H:i', $row['insert_datestamp']);
// calculate the difference between today (starting at midnight) and the posted date
// 86400 is the number of seconds in a day - should really use a constant here
$diff_days=round( (mktime(0, 0, 0) - $row['insert_datestamp']) / 86400);

switch($diff_days) :

  case 0: $postdate = 'Today'; break;
  case 1: $postdate= 'Yesterday'; break;
  case 2: $postdate='Day before yesterday'; break;
  default: $postdate=date('j F', $row['insert_datestamp']); break;

endswitch;

Better to use switch statement than multiple if elses.


You can just use the MySQL UNIX_TIMESTAMP() function to get the timestamp, and then use that in the PHP date() function, instead of calling strtotime() on the date string it usually returns.

Also, you can use the MySQL DATEDIFF() function to get the number of days between today and the date in the database.

SELECT DATEDIFF(NOW(), `my_date`) AS 'days_since_post',
       UNIX_TIMESTAMP(`my_date`) AS 'my_date_stamp'
FROM `my_table`;

And then in PHP:

<?php
switch($row['days_since_post']) {
    case 0: $post_day = 'Today'; break;
    case 1: $post_day = 'Yesterday'; break;
    default: $post_day = date('M jS, Y'); break;
}
$post_time = date('H:i', $row['my_date_stamp']);

echo "Posted: {$post_day} at {$post_time}";
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜