2013-02-21 130 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分@@ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

本课程可根据需要使用