使用php 獲取時(shí)間今天明天昨天時(shí)間戳的詳解
更新時(shí)間:2013年06月20日 11:13:28 作者:
本篇文章是對(duì)用php獲取時(shí)間今天明天昨天時(shí)間戳的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
使用php獲取時(shí)間今天明天昨天時(shí)間戳
2013-06-20 11:12
<?php
echo "今天:".date("Y-m-d")."<br>";
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";
echo "一周零兩天四小時(shí)兩秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";
echo "下個(gè)星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";
echo "上個(gè)周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "一個(gè)月前:".date("Y-m-d",strtotime("last month"))."<br>";
echo "一個(gè)月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";
strtotime()函數(shù)的作用是將日期時(shí)間描述解析為 Unix 時(shí)間戳
int strtotime ( string time [, int now] )
?>
本函數(shù)預(yù)期接受一個(gè)包含美國(guó)英語(yǔ)日期格式的字符串并嘗試將其解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對(duì)于 now 參數(shù)給出的時(shí)間,如果沒(méi)有提供此參數(shù)則用系統(tǒng)當(dāng)前時(shí)間。
-------------------------------------------------------------------
在PHP里得到前天和昨天的日期的代碼
前 天去面試的時(shí)候也是這樣,不過(guò)我當(dāng)時(shí)記不起來(lái)了.就記得MYSQL里面的date_sub(now(),'interval 1 day');date('Y/m/d h:i:s',mktime(date('h'), date('i'), date('s'), date('m') , date('d')+1, date('Y')));
--------------------------------------------------------------------------------
先得到今天的UNIXTIME
然后減去一天或兩天的秒數(shù)
把減后的UNIXTIME格式化成日期。
--------------------------------------------------------------------------------
以下為引用的內(nèi)容:
<?php
date_default_timezone_set('Asia/Shanghai');
#昨天
echo date("Y/m/d h:i:s",time()-24*60*60);
echo "<br>";
#前天
echo date("Y/m/d h:i:s",time()-2*24*60*60);
?>
--------------------------------------------------------------------------------
up
--------------------------------------------------------------------------------
方法有很多種啊, 我也介紹一種吧:
date("Y/m/d H:i:s", strtotime("1 days ago"));
date("Y/m/d H:i:s", strtotime("2 days ago"));
--------------------------------------------------------------------------------
date("Y/m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
--------------------------------------------------------------------------------
以前算時(shí)間總是很煩人,呵呵,學(xué)了了下,下面是下個(gè)星期現(xiàn)在的時(shí)間。
date_default_timezone_set('Asia/Shanghai');
$tmp = time()+60*60*24*7;
print date("m/d/Y H:i:s", $tmp);
--------------------------------------------------------------------------------
再加一個(gè):
$time_yes=localtime(time()-24*60*60, true);
$time_b_yes=localtime(time()-2*24*60*60, true);
$yesterday=$time_yes['tm_mday'];
$the_day_before_yes=$time_b_yes['tm_mday'];
--------------------------------------------------------------------------------
time()-86400 昨天的
以下為引用的內(nèi)容:
<?
//昨天
print date('Y-m-d' , strtotime('-1 day'));
//上星期
print date('Y-m-d' , strtotime('-1 week'));
//上個(gè)月
print date('Y-m-d' , strtotime('-1 month'));
//去年
print date('Y-m-d' , strtotime('-1 year'));
?>
--------------------------------------------------------------------------------
strtotime得到一個(gè)時(shí)間戳, 然后你自己格式化.
strtotime('yesterday');
strtotime('-2 day');
2013-06-20 11:12
<?php
echo "今天:".date("Y-m-d")."<br>";
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";
echo "一周零兩天四小時(shí)兩秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";
echo "下個(gè)星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";
echo "上個(gè)周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "一個(gè)月前:".date("Y-m-d",strtotime("last month"))."<br>";
echo "一個(gè)月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";
strtotime()函數(shù)的作用是將日期時(shí)間描述解析為 Unix 時(shí)間戳
int strtotime ( string time [, int now] )
?>
本函數(shù)預(yù)期接受一個(gè)包含美國(guó)英語(yǔ)日期格式的字符串并嘗試將其解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對(duì)于 now 參數(shù)給出的時(shí)間,如果沒(méi)有提供此參數(shù)則用系統(tǒng)當(dāng)前時(shí)間。
-------------------------------------------------------------------
在PHP里得到前天和昨天的日期的代碼
前 天去面試的時(shí)候也是這樣,不過(guò)我當(dāng)時(shí)記不起來(lái)了.就記得MYSQL里面的date_sub(now(),'interval 1 day');date('Y/m/d h:i:s',mktime(date('h'), date('i'), date('s'), date('m') , date('d')+1, date('Y')));
--------------------------------------------------------------------------------
先得到今天的UNIXTIME
然后減去一天或兩天的秒數(shù)
把減后的UNIXTIME格式化成日期。
--------------------------------------------------------------------------------
以下為引用的內(nèi)容:
<?php
date_default_timezone_set('Asia/Shanghai');
#昨天
echo date("Y/m/d h:i:s",time()-24*60*60);
echo "<br>";
#前天
echo date("Y/m/d h:i:s",time()-2*24*60*60);
?>
--------------------------------------------------------------------------------
up
--------------------------------------------------------------------------------
方法有很多種啊, 我也介紹一種吧:
date("Y/m/d H:i:s", strtotime("1 days ago"));
date("Y/m/d H:i:s", strtotime("2 days ago"));
--------------------------------------------------------------------------------
date("Y/m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
--------------------------------------------------------------------------------
以前算時(shí)間總是很煩人,呵呵,學(xué)了了下,下面是下個(gè)星期現(xiàn)在的時(shí)間。
date_default_timezone_set('Asia/Shanghai');
$tmp = time()+60*60*24*7;
print date("m/d/Y H:i:s", $tmp);
--------------------------------------------------------------------------------
再加一個(gè):
$time_yes=localtime(time()-24*60*60, true);
$time_b_yes=localtime(time()-2*24*60*60, true);
$yesterday=$time_yes['tm_mday'];
$the_day_before_yes=$time_b_yes['tm_mday'];
--------------------------------------------------------------------------------
time()-86400 昨天的
以下為引用的內(nèi)容:
<?
//昨天
print date('Y-m-d' , strtotime('-1 day'));
//上星期
print date('Y-m-d' , strtotime('-1 week'));
//上個(gè)月
print date('Y-m-d' , strtotime('-1 month'));
//去年
print date('Y-m-d' , strtotime('-1 year'));
?>
--------------------------------------------------------------------------------
strtotime得到一個(gè)時(shí)間戳, 然后你自己格式化.
strtotime('yesterday');
strtotime('-2 day');
您可能感興趣的文章:
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- php、mysql查詢當(dāng)天,查詢本周,查詢本月的數(shù)據(jù)實(shí)例(字段是時(shí)間戳)
- php獲取當(dāng)前月與上個(gè)月月初及月末時(shí)間戳的方法
- 時(shí)間戳與時(shí)間相互轉(zhuǎn)換(php .net精確到毫秒)
- PHP獲取毫秒級(jí)時(shí)間戳的方法
- PHP中UNIX時(shí)間戳和日期間的轉(zhuǎn)換與計(jì)算實(shí)例
- PHP時(shí)間戳 strtotime()使用方法和技巧
- php 獲取今日、昨日、上周、本月的起始時(shí)間戳和結(jié)束時(shí)間戳的方法
- php日期轉(zhuǎn)時(shí)間戳,指定日期轉(zhuǎn)換成時(shí)間戳
- php 計(jì)算兩個(gè)時(shí)間戳相隔的時(shí)間的函數(shù)(小時(shí))
- php獲取本年、本月、本周時(shí)間戳和日期格式的實(shí)例代碼
相關(guān)文章
PHP 返回13位時(shí)間戳的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇PHP 返回13位時(shí)間戳的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05php輸出控制函數(shù)和輸出函數(shù)生成靜態(tài)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了php輸出控制函數(shù)和輸出函數(shù)生成靜態(tài)頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06解決php-fpm.service not found問(wèn)題的辦法
這篇文章主要給大家介紹了解決php-fpm.service not found問(wèn)題的辦法,文中詳細(xì)介紹的解決這個(gè)問(wèn)題的思路與過(guò)程,分享出來(lái)給大家,如果有同樣問(wèn)題的朋友就不用到處找解決辦法了,下面來(lái)一起看看吧。2017-06-06php將HTML表格每行每列轉(zhuǎn)為數(shù)組實(shí)現(xiàn)采集表格數(shù)據(jù)的方法
這篇文章主要介紹了php將HTML表格每行每列轉(zhuǎn)為數(shù)組實(shí)現(xiàn)采集表格數(shù)據(jù)的方法,涉及php正則替換的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04解析PHP中empty is_null和isset的測(cè)試
本篇文章是對(duì)PHP中empty is_null和isse的測(cè)試進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php?overtrue/pinyin拓展實(shí)現(xiàn)漢字轉(zhuǎn)拼音
這篇文章主要為大家介紹了php?overtrue/pinyin拓展實(shí)現(xiàn)漢字轉(zhuǎn)拼音示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11解決Laravel blade模板轉(zhuǎn)義html標(biāo)簽的問(wèn)題
今天小編就為大家分享一篇解決Laravel blade模板轉(zhuǎn)義html標(biāo)簽的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09