PHP - 如何使用$ timestamp检查今天是星期一还是1月1日?

时间:2012-12-28 01:08:24

标签: php timestamp

我一直在线查看示例,我发现它们有点神秘或过度。

我需要做的是这样的事情:

$timestamp = time();

然后查明这一天是星期一还是月份?

我确信这是可能的,我只是不知道该怎么做。

6 个答案:

答案 0 :(得分:50)

实际上,您不需要时间戳变量,因为:

来自php.net的date函数:

  

使用返回根据给定格式字符串格式化的字符串   给定的整数时间戳或当前时间(如果没有时间戳)   给出。换句话说,timestamp是可选的,默认为该值   时间()。

if(date('j', $timestamp) === '1') 
    echo "It is the first day of the month today\n";

if(date('D', $timestamp) === 'Mon') 
    echo "It is Monday today\n";

答案 1 :(得分:7)

这应解决它:

$day = date('D');
$date = date('d')
if($day == Mon){
    //Code for monday
}
if($date == 01){
    //code for 1st fo the month
}
else{
    //not the first, no money for you =/
}

答案 2 :(得分:3)

这将从星期一从mysql中获取

$monday = 1; //tuesday= 2.. sunday = 7

    AND $monday = (date_format(from_unixtime(your_date_column),'%w')) 

OR days ..

$day = 1; ///1st in month

    AND $day = (date_format(from_unixtime(your_date_column),'%d')) 

只是知道

$date  = date("d"); //1st?
$dayinweek = date("w"); //monday? //as a number in a week what you need more then just "Monday" I guess..

答案 3 :(得分:1)

您可以使用:strtotime

$firstdaymonth = strtotime('first day this month');

答案 4 :(得分:1)

因为$ date可以是星期一或星期日。应该检查

public function getWeek($date){
    $date_stamp = strtotime(date('Y-m-d', strtotime($date)));

     //check date is sunday or monday
    $stamp = date('l', $date_stamp);      

    if($stamp == 'Mon'){
        $week_start = $date;
    }else{
        $week_start = date('Y-m-d', strtotime('Last Monday', $date_stamp));
    }


    if($stamp == 'Sunday'){
        $week_end = $date;
    }else{
        $week_end = date('Y-m-d', strtotime('Next Sunday', $date_stamp));
    }        
    return array($week_start, $week_end);
}

答案 5 :(得分:0)

由于 PHP> = 5.1 ,因此可以使用date('N'),它返回星期几的ISO-8601数字表示形式,其中1是星期一,7是星期日。

所以你可以做

if(date('N', $timestamp) === '1' || date('j', $timestamp) === '1')) {
    echo "Today it is Monday OR the first of the month";
}