Home > Backend Development > PHP Tutorial > php gets the timestamp of the first and last day of this week, month

php gets the timestamp of the first and last day of this week, month

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 08:57:04
Original
2223 people have browsed it
This article introduces the code for using PHP to obtain the timestamps of the first and last days of this week and this month, for reference by friends in need.

Use php to get the timestamps of the first and last days of this week and this month.

1, Get today’s time range:

<?php
$start = mktime(0,0,0,date("m"),date("d"),date("Y")); 
$end = mktime(0,0,0,date("m"),date("d")+1,date("Y")); 
Copy after login

2, get the timestamp of the first/last day of the week

<?php
$year = date("Y");
$month = date("m");
$day = date('w');
$nowMonthDay = date("t");

$firstday = date('d') - $day;
if(substr($firstday,0,1) == "-"){
 $firstMonth = $month - 1;
 $lastMonthDay = date("t",$firstMonth);
 $firstday = $lastMonthDay - substr($firstday,1);
 $time_1 = strtotime($year."-".$firstMonth."-".$firstday);
}else{
 $time_1 = strtotime($year."-".$month."-".$firstday);
}
  
$lastday = date('d') + (7 - $day);
if($lastday > $nowMonthDay){
 $lastday = $lastday - $nowMonthDay;
 $lastMonth = $month + 1;
 $time_2 = strtotime($year."-".$lastMonth."-".$lastday);
}else{
 $time_2 = strtotime($year."-".$month."-".$lastday);
}
Copy after login

3. Get the timestamp of the first/last day of this month

<?php
$year = date("Y");
$month = date("m");
$allday = date("t");
$strat_time = strtotime($year."-".$month."-1");
$end_time = strtotime($year."-".$month."-".$allday);
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template