国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP獲取特殊時(shí)間戳的方法整理

瀏覽:223日期:2022-06-06 18:52:38
目錄
  • 問(wèn)題描述
  • 解決方案
  • 今天時(shí)間戳與日期
    • 時(shí)間戳
    • 日期格式
  • 昨天時(shí)間戳與日期
    • 日期格式
  • 本周時(shí)間戳與日期
    • 時(shí)間戳
    • 日期格式
  • 本月時(shí)間戳和日期
    • 時(shí)間戳
    • 日期格式
  • 本季度時(shí)間戳和日期
    • 時(shí)間戳
    • 日期格式
  • 當(dāng)前年時(shí)間戳和日期
    • 時(shí)間戳
    • 日期格式
  • strtotime函數(shù)獲取描述對(duì)應(yīng)時(shí)間
    • 明天當(dāng)前時(shí)間
    • 昨天當(dāng)前時(shí)間
    • 下個(gè)星期當(dāng)前時(shí)間
    • 上個(gè)星期當(dāng)前時(shí)間
    • 下星期幾當(dāng)前時(shí)間
    • 上星期幾當(dāng)前時(shí)間
    • 時(shí)間戳轉(zhuǎn)日期格式
    • 日期格式轉(zhuǎn)時(shí)間戳
    • 獲取特定時(shí)間戳函數(shù)
  • 寫(xiě)在最后

    問(wèn)題描述

    時(shí)間在我們?nèi)粘5拇a編寫(xiě)中會(huì)是經(jīng)常出現(xiàn)的篩選或排序條件,尤其是一些特殊時(shí)間節(jié)點(diǎn)的時(shí)間顯得尤為突出,例如昨天,當(dāng)前日期,當(dāng)前月份,當(dāng)前季度,以及當(dāng)前年份的開(kāi)始以及結(jié)束的時(shí)間戳,今天對(duì)部分相對(duì)簡(jiǎn)便的方法進(jìn)行了部分整理。

    解決方案

    話不多說(shuō),稍微進(jìn)行分類,貼代碼。

    今天時(shí)間戳與日期

    時(shí)間戳

    當(dāng)前天的時(shí)間戳直接使用當(dāng)前時(shí)間格式,指定起始以及結(jié)束時(shí)間來(lái)實(shí)現(xiàn)快速拿到時(shí)間戳的效果。

     $startTime = strtotime(date("Y-m-d")."00:00:00");
     $overTime = strtotime(date("Y-m-d")."23:59:59");
    

    日期格式

    相應(yīng)的,咱們可以直接字符串拼接實(shí)現(xiàn)日期格式的顯示。

    //弱類型語(yǔ)言,直接拼接字符串
     $startDate=date("Y-m-d")." 00:00:00";
     $overDate=date("Y-m-d")." 00:00:00";
    

    昨天時(shí)間戳與日期

    時(shí)間戳

     $startTime = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
     $overTime = mktime(0,0,0,date("m"),date("d"),date("Y"))-1;
    

    日期格式

    方法一: 根據(jù)時(shí)間戳轉(zhuǎn)日期格式

    //根據(jù)上面的時(shí)間戳進(jìn)行格式轉(zhuǎn)換
     $startDate=date("Y-m-d H:i:s",$startTime);
     $overDate =date("Y-m-d H:i:s",$overTime);
    

    新想法:根據(jù)首先獲取當(dāng)前天日期,然后使用date函數(shù)進(jìn)行時(shí)間格式轉(zhuǎn)換

    //獲取當(dāng)前日期的天數(shù)的數(shù)值減一之后就是昨天啦
     $time=date("d")-1;
     $startDate=date("Y-m-".$time." 00:00:00",time());
     $overDate=date("Y-m-".$time." 23:59:59",time());
    

    但是在月初時(shí)會(huì)出現(xiàn)日期為0的異常,除了進(jìn)行判斷,不知道有沒(méi)有其他簡(jiǎn)便的方法可以解決,不然還是時(shí)間戳轉(zhuǎn)日期格式比較簡(jiǎn)便,希望有簡(jiǎn)單解決辦法的大佬給點(diǎn)新想法。

    本周時(shí)間戳與日期

    時(shí)間戳

    date( )函數(shù)中 date(‘w’) 可以獲取今天是本周第幾天,通過(guò)簡(jiǎn)單處理就可以得到本周的起始以及結(jié)束時(shí)間。

    這種思路和方法可以推廣到上周的起始和結(jié)束時(shí)間。

    方法一:

    //本周開(kāi)始時(shí)間戳
    $startTime = mktime(0,0,0,date("m"),date("d")-date("w")+1,date("y"));
    //本周結(jié)束時(shí)間戳
    $overTime = mktime(23,59,59,date("m"),date("d")-date("w")+7,date("y"));
    

    方法二:

     $nowDate = date("Y-m-d");
     $week = date("w",strtotime($nowDate));
     $startTime = strtotime("$nowDate -".($week ? $week - 1 : 6)." days");//本周第一天
     $overTime = $start_time + 86400*7 -1; //本周最后一天
    

    日期格式

    使用日期格式函數(shù)轉(zhuǎn)換時(shí)間戳,也可以用上面的方法進(jìn)行date()函數(shù)中格式,進(jìn)行轉(zhuǎn)換。

    //本周開(kāi)始時(shí)間戳
    $startTime = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-date("w")+1,date("y")));
    //本周結(jié)束時(shí)間戳
    $overTime = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("y")));
    

    本月時(shí)間戳和日期

    時(shí)間戳

    //本月起始時(shí)間時(shí)間戳
    $startTime =mktime(0,0,0,date("m"),1,date("Y"));  
    //本月結(jié)束時(shí)間時(shí)間戳
    $overTime =mktime(23,59,59,date("m"),date("t"),date("Y")); 
    

    日期格式

    使用date( )函數(shù)進(jìn)行時(shí)間戳轉(zhuǎn)換日期格式。

    //本月起始時(shí)間日期格式
    $startTime = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),1,date("Y")));  
    //本月結(jié)束時(shí)間日期格式
    $overTime = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))); 
    

    本季度時(shí)間戳和日期

    時(shí)間戳

     //獲取當(dāng)前季度
    $season = ceil((date("m"))/3);
     //當(dāng)前季度開(kāi)始時(shí)間戳
    $startTime = mktime(00,00,00,$season*2+1,1,date("Y"));
     //獲取當(dāng)前季度結(jié)束時(shí)間戳
    $overTime = mktime(23,59,59,$season*3,date("t",mktime(0, 0 , 0,$season*3,1,date("Y"))),date("Y"));
    

    日期格式

    把上面的時(shí)間戳轉(zhuǎn)換為日期格式

    date("Y-m-d",$startTime)
    date("Y-m-d",$overTime)
    

    當(dāng)前年時(shí)間戳和日期

    時(shí)間戳

    //本年開(kāi)始  
    $startTime  = strtotime(date("Y",time())."-1"."-1"); 
    //本年結(jié)束
    $overTime  = strtotime(date("Y",time())."-12"."-31");  
    

    日期格式

    //本年開(kāi)始  
    $startTime  = date("Y-m-d H:i:s",strtotime(date("Y",time())."-1"."-1"));
    //本年結(jié)束
    $overTime  =  date("Y-m-d H:i:s",strtotime(date("Y",time())."-12"."-31"));  
    

    strtotime函數(shù)獲取描述對(duì)應(yīng)時(shí)間

    明天當(dāng)前時(shí)間

    strtotime("+1 day")//時(shí)間戳
    
    date("Y-m-d H:i:s",strtotime("+1 day"))//日期格式
    

    昨天當(dāng)前時(shí)間

    strtotime("-1 day")//時(shí)間戳
    
    date("Y-m-d H:i:s",strtotime("-1 day"))//日期格式
    

    下個(gè)星期當(dāng)前時(shí)間

    strtotime("+1 week")//時(shí)間戳
    
    date("Y-m-d H:i:s",strtotime("+1 week"))//日期格式
    

    上個(gè)星期當(dāng)前時(shí)間

    strtotime("-1 week")//時(shí)間戳
    
    date("Y-m-d H:i:s",strtotime("-1 week"))//日期格式
    

    下星期幾當(dāng)前時(shí)間

    strtotime("next Thursday")//時(shí)間戳
    
    date("Y-m-d H:i:s",strtotime("next Thursday"))//日期格式
    

    上星期幾當(dāng)前時(shí)間

    strtotime("last Thursday")//時(shí)間戳
    
    date("Y-m-d H:i:s",strtotime("last Thursday"))//日期格式
    

    時(shí)間戳轉(zhuǎn)日期格式

    $timestamp =1664170375;//當(dāng)前時(shí)間戳
    date("Y-m-d H:i:s",$timestamp);//轉(zhuǎn)換為日期格式
    

    日期格式轉(zhuǎn)時(shí)間戳

    $time = "2022-09-26 23:31:59";//時(shí)間格式參數(shù)
    strtotime($time);//轉(zhuǎn)換為時(shí)間戳
    

    獲取特定時(shí)間戳函數(shù)

    /**特定時(shí)間戳函數(shù)
     * @param $targetTime
     */
    function gettimestamp($targetTime){
        switch ($targetTime){
    case "today"://今天
        $timeamp["start"] = strtotime(date("Y-m-d"));
        $timeamp["over"] = strtotime(date("Y-m-d",strtotime("+1 day")));
        break;
    case "yesterday"://昨天
        $timeamp["start"] = strtotime(date("Y-m-d",strtotime("-1 day")));
        $timeamp["over"] = strtotime(date("Y-m-d"));
        break;
    case "beforyesterday"://前天
        $timeamp["start"] = strtotime(date("Y-m-d",strtotime("-2 day")));
        $timeamp["over"] = strtotime(date("Y-m-d",strtotime("-1 day")));
        break;
    case "beforweek"://本周
        $timeamp["start"] = strtotime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))));
        $timeamp["over"] = strtotime(date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))));
        break;
    case "nowmonth"://本月
        $timeamp["start"] = strtotime(date("Y-m-01"));
        $timeamp["over"] = strtotime(date("Y-m-d",strtotime("+1 day")));
        break;
    case "permonth"://上月
        $timeamp["start"] = strtotime(date("Y-m-01",strtotime("-1 month")));
        $timeamp["over"] = strtotime(date("Y-m-01"));
        break;
    case "preweek"://上周 注意我們是從周一開(kāi)始算
        $timeamp["start"] = strtotime(date("Y-m-d",strtotime("-2 week Monday")));
        $timeamp["over"] = strtotime(date("Y-m-d",strtotime("-1 week Monday +1 day")));
        break;
    case "nowweek"://本周
        $timeamp["start"] = strtotime(date("Y-m-d",strtotime("-1 week Monday")));
        $timeamp["over"] = strtotime(date("Y-m-d",strtotime("+1 day")));
        break;
    case "preday"://30
        $timeamp["start"] = strtotime(date("Y-m-d"),strtotime($param." day"));
        $timeamp["end"] = strtotime(date("Y-m-d"));
        break;
    case "nextday"://30
        $timeamp["start"] = strtotime(date("Y-m-d"));
        $timeamp["over"] = strtotime(date("Y-m-d"),strtotime($param." day"));
        break;
    case "preyear"://去年
        $timeamp["start"] = strtotime(date("Y-01-01",strtotime("-1 year")));
        $timeamp["over"] = strtotime(date("Y-12-31",strtotime("-1 year")));
        break;
    case "nowyear"://今年
        $timeamp["start"] = strtotime(date("Y-01-01"));
        $timeamp["over"] = strtotime(date("Y-m-d",strtotime("+1 day")));
        break;
    case "quarter"://季度
        $quarter = ceil((date("m"))/3);
        $timeamp["start"] = mktime(0, 0, 0,$quarter*3-2,1,date("Y"));
        $timeamp["over"] = mktime(0, 0, 0,$quarter*3+1,1,date("Y"));
        break;
    default:
        $timeamp["start"] = strtotime(date("Y-m-d"));
        $timeamp["over"] = strtotime(date("Y-m-d",strtotime("+1 day")));
        break;
        }
        return $timeamp;
    }

    寫(xiě)在最后

    小發(fā)現(xiàn):在進(jìn)行測(cè)試的時(shí)候發(fā)現(xiàn)了 date()函數(shù)比較有意思的地方,可以直接拼接結(jié)果,當(dāng)你把y-m-d h:i:s中的一部分寫(xiě)死后仍然是可以執(zhí)行的,結(jié)果就是你寫(xiě)死的數(shù)值,后面有機(jī)會(huì)深入研究下底層代碼,好像是在C語(yǔ)言中,結(jié)構(gòu)體來(lái)實(shí)現(xiàn)日期以及時(shí)間戳的格式,傳參是進(jìn)行了判斷,所以可以達(dá)到不同形式的顯示。strtotime() 函數(shù)也很巧妙,牛哇牛哇

    到此這篇關(guān)于PHP獲取特殊時(shí)間戳的方法整理的文章就介紹到這了,更多相關(guān)PHP獲取特殊時(shí)間戳內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

    標(biāo)簽: PHP
    主站蜘蛛池模板: 商洛市| 襄樊市| 贵州省| 大理市| 托克逊县| 绥德县| 丹江口市| 武功县| 芦山县| 和平区| 云安县| 汝南县| 社旗县| 苏尼特左旗| 武汉市| 石家庄市| 兴业县| 女性| 思南县| 南通市| 金乡县| 且末县| 中山市| 邯郸县| 宜春市| 洛隆县| 石阡县| 伊吾县| 平遥县| 外汇| 陇南市| 汉沽区| 南溪县| 孝感市| 红桥区| 汨罗市| 琼结县| 通州市| 德令哈市| 濮阳市| 榕江县|