簡體   English   中英

strtotime 沒有正確轉換日期

[英]strtotime is not converting date correctly

下面是我用來識別存儲在數組中的連續日期范圍的代碼

$lastDate = null;
$currentRange = array();
$holidays=array("2022-12-15","2022-12-16","2022-12-17","2022-12-28","2022-12-29");
    
    
    foreach($holidays as $holiday)
    {
        
        $time_stamp=strtotime($holiday);
        
       
        if($time_stamp<=$fromdate)
        {
             
             if (null === $lastDate)
                 {
                    $currentRange[] = $time_stamp;
                    
                 }
            else
                 {

                    // get the DateInterval object
                    $interval = ($time_stamp-$lastDate)/86400;
                        
                    // // DateInterval has properties for 
                    // // days, weeks. months etc. You should 
                    // // implement some more robust conditions here to 
                    // // make sure all you're not getting false matches
                    // // for diffs like a month and a day, a year and 
                    // // a day and so on...

                     if ($interval == 1) {
                        // add this date to the current range
                        $currentRange[] = date('m-d-Y',$time_stamp);   
                        
                        } else {
                        // store the old range and start anew
                        //$ranges[] = $currentRange;
                        $currentRange = array(date('m-d-Y',$time_stamp));
                        
                    }
                  }
                  $lastDate = $time_stamp;
        }

當我顯示 $currentrange 輸出是:[1671042600, '12-16-2022', '12-17-2022']

第一個時間戳未轉換為日期時間格式。

此外,返回的第一個時間戳“1671042600”對應於“12-14-2022”,而它應該是“12-15-2022”。

我無法弄清楚為什么會出現這種奇怪的響應,只有第一個時間戳未正確轉換為日期,而其他時間戳已正確轉換為日期

改變

$time_stamp=strtotime($holiday);

$time_stamp = (strtotime($holiday)?strtotime($holiday):$holiday);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM