我试图为一周内的元素创建一个选择器。我得到一个时间戳像这样的Sun, 02 Jun 2013 22:05:00 GMT
,但选择器不应该受到时间
如
<?
$curdate = date( 'D, d M Y H:I:s' );
$olddate = "Sun, 02 Jun 2013 22:05:00 GMT";
if($curdate < $olddate){
// Date is with in a week
} else {
// Date is older then a week
}
但是在这种情况下,它不应该受到每天在线时间的影响。
PHP的strtotime()
函数就是你要找的。
echo date('jS F Y H:i.s', strtotime('-1 week'));
您可以在strtotime()
函数中输入许多不同的字符串,例如:
strtotime('yesterday');
strtotime('-2 days ago');
strtotime('+5 days');
您应该只在比较日期之外创建日期。之后,您应该创建给定日期Sun, 02 Jun 2013 22:05:00 GMT
的时间戳,并且应该将其转换为仅包含日期的日期字符串。然后从这个....创建另一个时间戳
如果你明白我的意思…这应该可以工作:
<?php
// First create the date
$date = 'Sun, 02 Jun 2013 22:05:00 GMT';
// To a timestamp
$t_date = strtotime($date);
// Noew remove the seconds: First create a new date, with a timestamp of the give date.
// After that create a datestring with only the date
$date = date("jS F Y", $t_date);
// And create a new timestamp
$t_date = strtotime($date);
// One week back: time - 60 seconds * 60 minutes * 24 hours * 7 days * -1 to get backwards
// And we only create a date of this
$weekback = date('jS F Y', time() + (60 * 60 * 24 * -7) );
// Create a timestamp
$t_weekback = strtotime($weekback);
// Debug
echo "Date: $date<br/>Date (UTC): $t_date<br/>";
echo "Last week: $weekback<br/>Last week (UTC): $t_weekback<br/>";
if ($t_date <= $t_weekback) {
//Date is older then a week
echo "Outside a week: last week($t_date) <= The date($t_weekback)";
}else{
//Date is within a week
echo "Within a week: $t_date > $t_weekback";
}
?>
当您使用PHP>= 5.3时,您可以使用以下命令:
<?php
$date = new DateTime('Sun, 02 Jun 2013 22:05:00 GMT');
$interval = new DateInterval('P1W');
if(new DateTime() < $date->add($interval)){
//date is with in a week
}{
//date is older then a week
}