How to get the year, month, day timestamp and date in php

不言
Release: 2023-04-03 08:30:01
Original
6011 people have browsed it

PHP obtains the timestamp through the PHP time() function, and PHP obtains the date through the PHP data() function. How are they implemented? Specific examples are given below.

Set time zone

date_default_timezone_set("Asia/Shanghai"); 
date_default_timezone_set('PRC');//这两种方法效果相同
Copy after login

Time stamp conversion can be done by using date('Y-m-s h:i:s', specific timestamp)
Date conversion Timestamp, use strtotime("date()").

Timestamp format

//获取今日开始时间戳和结束时间戳  
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));  
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;  

//获取昨日起始时间戳和结束时间戳  
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));  
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;  

//获取本周起始时间戳和结束时间戳   
$beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));  
$endThisweek=time();  

//获取上周起始时间戳和结束时间戳  
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));  
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));  

//获取本月起始时间戳和结束时间戳  
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));  
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));  

 //上个月的起始时间:  
 $begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));  
$end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));  

$begin_year = strtotime(date("Y",time())."-1"."-1"); //本年开始  
$end_year = strtotime(date("Y",time())."-12"."-31"); //本年结束  
//现在的时间到第二天凌晨相差的时间戳  
$time = (strtotime(date('Y-m-d'))+3600*24) - time() ;
Copy after login

Date format

    echo &#39;<br>上周起始时间:<br>&#39;;
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n";
    echo &#39;<br>本周起始时间:<br>&#39;;
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n";

    echo &#39;<br>上月起始时间:<br>&#39;;
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n";
    echo &#39;<br>本月起始时间:<br>&#39;;
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n";    //本年起始
    echo date(‘Y-01-01’); 
    //结束日期 
    echo date(‘Y-12-31’);
Copy after login

Related recommendations:

Share the method of outputting time by date() function in PHP

The above is the detailed content of How to get the year, month, day timestamp and date in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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