PHP - 如何使用 $timestamp 来检查今天是星期一还是一个月的第一天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14063336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - how to use $timestamp to check if today is Monday or 1st of the month?
提问by Genadinik
I have been looking through examples online, and I am finding them a bit cryptic or overkill.
我一直在网上浏览示例,我发现它们有点神秘或矫枉过正。
What I need to do is something like this:
我需要做的是这样的:
$timestamp = time();
and then find out if the day is a Monday or a fist of the month?
然后找出这一天是星期一还是一个月的第一天?
I am sure it is possible, I am just not sure how to do that.
我确信这是可能的,我只是不知道如何做到这一点。
回答by Roman Newaza
Actually, you don't need timestamp variable because:
实际上,您不需要时间戳变量,因为:
Exerpt from datefunction of php.net:
摘自php.net的日期函数:
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给定时间戳)。换句话说,timestamp 是可选的,默认为 time() 的值。
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";
回答by DalekSall
This should solve it:
这应该解决它:
$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 =/
}
回答by Xfile
This will grab.. Monday from mysql
这将抓住 .. 星期一从 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'))
JUST TO KNOW
想知道
$date = date("d"); //1st?
$dayinweek = date("w"); //monday? //as a number in a week what you need more then just "Monday" I guess..
回答by Phillip Plum
回答by user2214236
Because $date can monday or sunday. Should be check it
因为 $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);
}
回答by kregus
Since PHP >= 5.1it is possible to use date('N'), which returns an ISO-8601 numeric representation of the day of the week, where 1 is Monday and 7 is Sunday.
由于PHP >= 5.1可以使用date('N'),它返回一周中某天的 ISO-8601 数字表示,其中 1 是星期一,7 是星期日。
So you can do
所以你可以做
if(date('N', $timestamp) === '1' || date('j', $timestamp) === '1')) {
echo "Today it is Monday OR the first of the month";
}

