我一直在网上浏览示例,我发现它们有点神秘或矫枉过正。
我需要做的是这样的:
$timestamp = time();
然后找出这一天是星期一还是本月的第一天?
我相信这是可能的,我只是不知道该怎么做。
实际上,您不需要时间戳变量,因为:
摘自 php.net 的日期函数:
返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给出时间戳)。换句话说,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";
这应该解决它:
$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 =/
}
这将抓住..星期一从mysql
$monday = 1; //tuesday= 2.. sunday = 7
AND $monday = (date_format(from_unixtime(your_date_column),'%w'))
或天..
$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..
你可以使用:strtotime
$firstdaymonth = strtotime('first day this month');
因为 $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);
}
由于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";
}