How to get week, month start and end timestamps in php?
You need to know how to get a week, month start and end timestamps in PHP when want to query new users, posts, or other data that happen in today, this week or this month. The following codes will help you.
Timestamps of Today:
$start = strtotime(date("Y-m-d 0:0:0")); | |
$end = strtotime(date("Y-m-d 23:59:59")); |
Start and end timestamps of the current week:
if (date('N', time()) == 1){ //today is Monday | |
$startTimeStr = date("Y-m-d 00:00:00", time()); | |
}else{ | |
$startTimeStr = date("Y-m-d 00:00:00", strtotime('next Monday') - 7*24*3600); | |
}
| |
if (date('N', time()) == 7){ //today is Sunday | |
$endTimeStr = date("Y-m-d 23:59:59", time()); | |
}else{ | |
$endTimeStr = date("Y-m-d 23:59:59", strtotime('next Sunday')); | |
}
| |
$start = strtotime($startTimeStr); | |
$end = strtotime($endTimeStr); |
Start and end timestamps of the current month:
$start = mktime(0, 0, 0, date("n"), 1, date("Y") ); | |
$end = mktime(23, 59, 59, date("n"), date("t"), date("Y") ); |

Post a Comment