2013-02-21 123 views
3

有誰知道是否已經有一種格式化日期到某個精度的方法,沒有我寫一些東西來重建給定的格式?PHP日期格式精度

例如,說我有什麼格式的日期,但我想將其轉換爲YMD H:我:s格式,使用PHP DateTimeFormat(請參閱下面的PHP網站爲例)

$date = new DateTime('2000-01-01'); 
    echo $date->format('Y-m-d H:i:s'); 
    Result: 2000-01-01 00:00:00 

如果它從未包含在原始日期中,我真正希望它做的不是返回結果中的H:i:s部分。

此刻,我能想到這樣做的唯一方法是使用date_parse_from_format,制定缺少什麼,然後做一些字符串處理上的日期格式「YMD H:我:s」來省略部分我不需要(因爲這個日期格式可能是任何東西,所以亂碼字符串格式化)。

我只是不想重新發明輪子,如果已經有一個功能,我已經錯過了。

鉭,喬

編輯:要儘量更清晰,日期格式字符串和$日期的精度是可變的,所以我不能硬編碼格式字符串,我不知道會不會精確的$日期,直到我得到它。

+1

如果您的潛在問題我如果你的用戶輸入日期不同,那麼你可以考慮在你的表單中使用日期選擇器,然後所有的日期都會以相同的方式格式化。 – starshine531 2013-02-21 12:58:18

回答

0

的preg_match是你在找什麼,具體有:

if(preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/',$date)){ 
    //here do the conversion for "Y-m-d H:i:s" 
}else{ 
    //here do the conversion for "Y-m-d" 
} 
+0

對不起,我不是很清楚,我有一個完整的日期格式,我需要使用(作爲一個變量,特定於我保存數據的數據源),我不知道日期的精確程度如何將需要轉換,這將取決於用戶的輸入。我可以在日期格式上做一些字符串處理,但我只是想知道是否已經有了一個函數。 – Joanne 2013-02-21 12:27:50

+0

我已經在答案中做了一個編輯..檢查它Joanne – 2013-02-21 12:32:56

+0

嗨,我想我需要使用類似的東西,但日期可以是任何格式,所以它可能是像「m/d/yh :一世」。我只是想知道是否有一個PHP函數我錯過了,它會以某種精度返回日期,而不是使用preg_match搞亂,並嘗試從格式字符串中刪除數據。我想這可能不是:謝謝你的幫助,但至少顯示我在正確的軌道上:) – Joanne 2013-02-21 12:36:09

0

我認爲你必須知道什麼格式是你的約會。

<?php 
echo 'Current time: ' . date('Y-m-d H:i:s') . "\n"; 

$format = 'Y-m-d'; 
$date = DateTime::createFromFormat($format, '2009-02-15'); 
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n"; 

$format = 'Y-m-d H:i:s'; 
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17'); 
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n"; 

$format = 'Y-m-!d H:i:s'; 
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17'); 
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n"; 

$format = '!d'; 
$date = DateTime::createFromFormat($format, '15'); 
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n"; 
?> 
0

我不認爲這是最有效的方法,但它似乎工作。它可以調整,以允許不同級別的精度。 (我挑Y-m-d H:i:sY-m-d H:iY-m-d。我沒想到Y-m-d H將是有益的,但也許你會喜歡Y-m-d ha。如果是這樣,這將是很容易添加。)

<?php 
// Output dates at arbitrary precision. 
header ('Content-Type: text/plain'); 

$input = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d'); 
$date = new DateTime($input); 
echo format_date($date); 

function format_date($date) { 
    switch ($date->format('s')) { 
    case '00': 
     switch ($date->format('H')) { 
     case '00': 
      return $date->format('Y-m-d'); 
     default: 
      return $date->format('Y-m-d H:i'); 
     } 
    default: 
     return $date->format('Y-m-d H:i:s'); 
    } 
} 

當然,這並不意味着該人實際上想要一個恰好午夜的時間,並且明確地這樣說。我不知道你對此案的覆蓋程度如何。

0

這是將時間和日期保存爲unix時間戳而不是其他格式的最佳方式。

我已經創建了一個類來處理日期和時間在PHP中。它易於使用,非常有用

在你的情況下,你可以使用提供的setter設置年,月,日,小時,分和秒,而不是擴展一個公共函數來獲取日期和時間作爲你的預期格式。

<?php 
define("NEW_LINE", "</BR>"); 
class scTimestamp 
{ 
    private $timestamp; 
    private $year; 
    private $month; 
    private $day; 
    private $hour; 
    private $minute; 
    private $second; 

    public function __construct() 
    { 
     register_shutdown_function(array($this,'__destruct')); 
     $this->setTimestamp($_SERVER['REQUEST_TIME']); 
    } 
    public function __destruct() 
    { 
     unset($this->timestamp); 
     unset($this->year); 
     unset($this->month); 
     unset($this->day); 
     unset($this->hour); 
     unset($this->minute); 
     unset($this->second); 
    } 
    private function rebuildTimestampFromDate() 
    { 
     $this->timestamp=mktime($this->hour,$this->minute,$this->second,$this->month,$this->day,$this->year); 
    } 
    private function rebuildDateFromTimestamp() 
    { 
     $this->day=date('j',$this->timestamp); 
     $this->month=date('n',$this->timestamp); 
     $this->year=date('Y',$this->timestamp); 
     $this->hour=date('g',$this->timestamp); 
     $this->minute=date('G',$this->timestamp); 
     $this->second=date('s',$this->timestamp);  
    } 
    public function setTimestamp($tempTimestamp) 
    { 
     $this->timestamp=$tempTimestamp;  
     $this->rebuildDateFromTimestamp(); 
    } 
    public function getTimestamp() 
    { 
     return $this->timestamp;  
    } 
    public function setYear($tempYear) 
    { 
     $this->year = $tempYear; 
     $this->rebuildTimestampFromDate(); 
    } 
    public function getYear() 
    { 
     return $this->year; 
    } 
    public function setMonth($tempMonth) 
    { 
     $this->month = $tempMonth; 
     $this->rebuildTimestampFromDate(); 
    } 
    public function getMonth() 
    { 
     return $this->month; 
    } 
    public function setDay($tempDay) 
    { 
     $this->day=$tempDay; 
     $this->rebuildTimestampFromDate(); 
    } 
    public function getDay() 
    { 
     return $this->day; 
    } 
    public function setHour($tempHour) 
    { 
     $this->hour = $tempHour; 
     $this->rebuildTimestampFromDate(); 
    } 
    public function getHour() 
    { 
     return $this->hour; 
    } 
    public function setMinute($tempMinute) 
    { 
     $this->minute = $tempMinute; 
     $this->rebuildTimestampFromDate(); 
    } 
    public function getMinute() 
    { 
     return $this->minute; 
    } 
    public function setSecond($tempSecond) 
    { 
     $this->second = $tempSecond; 
     $this->rebuildTimestampFromDate(); 
    } 
    public function getSecond() 
    { 
     return $this->second; 
    } 


    public function getDateDifferenceFromNow() 
    { 
     return $this->getDateDifferenceFrom($_SERVER['REQUEST_TIME']); 
    } 
    public function getDateDifferenceFrom($fromDate) 
    { 
     $return=""; 
     $sec=" Second"; 
     $min=" Minute"; 
     $hrs=" Hour"; 
     $before=" Before"; 
     $difference=$fromDate-$this->getTimestamp(); 
     if($difference<0) 
      $return.="In the Future"; 
     else if($difference<60) 
     { 
      if($difference>1) 
       $sec.="s"; 
      $return.= $difference.$sec.$before; 
     } 
     else if($difference<3600) 
     { 
      $difference=intval($difference/60); 
      if($difference>1) 
       $min.="s"; 
      $return.=$difference.$min.$before; 
     } 
     else if($difference<86400) 
     { 
      $difference=intval($difference/3600); 
      if($difference>1) 
       $hrs.="s"; 
      $return= $difference.$hrs.$before; 
     } 
     else if($difference<604800) 
     { 
      $return.= date("l g:i a",$this->getTimestamp()); 
     } 
     else if($difference<28512000) 
     { 
      $return.= date("F j",$this->getTimestamp()); 
     } 
     else 
     { 
      $return.= date("F j, Y, g:i a",$this->getTimestamp()); 
     } 
     return $return; 
    } 
    public function getDateAsString() 
    { 
     return date("F j, Y",$this->getTimestamp()); 
    } 
    public function getDateTimeAsString() 
    { 
     return date("F j, Y, g:i a",$this->getTimestamp()); 
    } 


    public function __toString() 
    { 
     $return = NEW_LINE."^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"; 
     $return.= NEW_LINE." ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^".NEW_LINE; 
     $return.= NEW_LINE."@@ Timestamp: ".$this->getTimestamp()." @@".NEW_LINE; 
     $return.= NEW_LINE."@@ Date: ".$this->getDateTimeAsString()." @@".NEW_LINE; 
     $return.= NEW_LINE." ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^".NEW_LINE; 
     return $return; 
    } 

} 
?> 

一旦納入它可以被用來作爲後續

include_once("scTimestamp.php"); 
$test=new scTimestamp(); 

echo $test->getDateAsString(); 

$test->setTimestamp(1210203200); 

echo $test->getDateDifferenceFromNow(); 

echo $test; 

$test->setTimestamp(121020320022); 
echo $test->getYear(); 

echo $test; 

而結果會這樣。

6月26日,2015May 7,2008年,下午11時33 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ @@時間戳:1210203200 @@ @@日期:2008年5月7日,晚上11:33 @@ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^5804 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@時間戳:121020320022 @ @ @@日期:5804年12月25日,上午3點33分@@ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

本課程可根據需要使用