简体   繁体   English

查找2天前,昨天和今天的过去一周以及PHP日期

[英]Find past week, 2 days ago, yesterday and today with a PHP date

I am trying to use the chartist JS script in order to fill up a chart. 我正在尝试使用Chartist JS脚本来填充图表。 My need is to show entries all the way back til past week. 我的需要是直到上周一直显示条目。 But I'd like to separate them in "Past Week, 2 Days Ago, Yesterday and Today". 但是我想在“过去两周,昨天和今天”中将它们分开。

As I get all the entries from the database, there is a field that contains a datetime, I'd like to find a flexible way to compare against it for each above case and show the entries or not. 当我从数据库中获取所有条目时,有一个包含日期时间的字段,我想找到一种灵活的方法来比较上述每种情况,并显示或不显示这些条目。

Tried something like, 尝试过类似的东西

$entrydate = explode(' ', $entry['datetime'])[0];

if (date('Y-m-d', strtotime('-7 days', strtotime($entrydate)))) {
    echo 'past week';
} else if (date('Y-m-d', strtotime('-2 days', strtotime($entrydate)))) {
    echo '2 days ago';
} else if (date('Y-m-d', strtotime('-1 days', strtotime($entrydate)))) {
    echo 'yesterday';
} else {
    echo 'today'; //...
}

but didn't work. 但是没有用

Today: 今天:

date("Y-m-d");

Yesterday: 昨天:

date("Y-m-d", strtotime("yesterday"));

7 Days ago: date("Ymd", strtotime("-7 days")); 7天前:date(“ Ymd”,strtotime(“-7 days”));

Last Monday: 上周一:

date("Y-m-d", strtotime("last Monday"));

strtotime documentation strtotime文档

To find Yesterday in relation to another day... 要查找与另一天有关的昨天...

$yesterday = date("Y-m-d", strtotime("2014-12-31") - 86400);

$sevendaysago = date("Y-m-d", strtotime("2014-12-31") - 7*86400);

I would not convert the dates to their textual representations. 我不会将日期转换为其文本表示形式。 Instead I would use timestamps and an array to match timestamps to texts. 相反,我将使用时间戳和数组将时间戳与文本进行匹配。

Something like: 就像是:

$times = array(
   86400 => 'yesterday',
  172800 => 'two days ago'
  // etc
);

$time = strtotime($entrydate);

if ( $time < (time() - 172800) ) {
  echo $times[172800];
} elseif ( $time < (time() - 86400) ) {
  echo $times[86400];
}
// etc.

You could probably do this in a loop or more intelligently to avoid all the repetition, but this is the general idea. 您可能可以循环执行此操作,也可以更智能地执行此操作以避免所有重复,但这是总的思路。

You can use the DateTime object. 您可以使用DateTime对象。 I find DateInterval pretty useful sometimes. 我发现DateInterval有时很有用。

$yourDate = "2014-12-28"; // The date to compare
$now = new DateTime(date("Y-m-d"));
$from = new DateTime($yourDate);
$diff = $now->diff($from);
$diffByDays = $diff->days;

switch($diffByDays) {
    case 7:
        // One Week
        break;
    case 1:
        // Yesterday
        break;
    default:
        break;
}

Result with print_r($diff): 使用print_r($ diff)的结果:

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 3
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 1
    [days] => 3
)

date() is going to return a string on success, FALSE on failure. date()将在成功时返回字符串,在失败时返回FALSE。 The IF statements and ELSE IF statements are not going to evaluate as TRUE and print the desired results. IF语句和ELSE IF语句将不会被评估为TRUE并输出所需的结果。 Without running the code I'd expect that it only prints the final ELSE condition. 如果不运行代码,我希望它只显示最终的ELSE条件。

date( "Y-m-d", strtotime( "today" ) );
date( "Y-m-d", strtotime( "today -2 days" ) );
date( "Y-m-d", strtotime( "today -7 days" ) );
date( "Y-m-d", strtotime( "today -15 days" ) );
date( "Y-m-d", strtotime( "today -1 month" ) );

;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM