2014-01-27 52 views
-4

我從數據庫中獲取數據,它給了我一個像2014-01-06 06:13:06這樣的格式的日期。我想將此日期轉換爲格式Jan 6th 2014如何在PHP中轉換dateformat?

這裏是我的代碼:

<?php 
$query="SELECT Min(transaction_date) as start,Max(transaction_date) as end from transactions"; 
$result1= mysql_query($query) or die(mysql_error()); 
$ors1=mysql_fetch_array($result1); 
$date= $ors1['start']; 
echo date_format($date, 'g:ia \o\n l jS F Y'); 
?> 
+2

'echo date('g:ia \ o \ n l jS F Y',strtotime($ ors1 ['start']))' – softsdev

回答

1

嘗試這一個

<?php 
    $query="SELECT Min(transaction_date) as start,Max(transaction_date) as end from transactions"; 
    $result1= mysql_query($query) or die(mysql_error()); 
    $ors1=mysql_fetch_array($result1); 
    $date= $ors1['start']; 
    echo date('dS M Y' ,strtotime($date)); 
?> 

參考URL:PHP Documentation

0

使用strtotime擺脫時間字符串表示的時間戳。 然後使用date將時間戳格式化爲您想要的任何格式。

例如

$formatted_date = date('g:ia \o\n l jS F Y', strtotime('2014-01-06 06:13:06 ')); 
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"; 
?> 

上面的例子將輸出類似於:

Current time: 2010-04-23 10:29:35 
Format: Y-m-d; 2009-02-15 10:29:35 
Format: Y-m-d H:i:s; 2009-02-15 15:16:17 
Format: Y-m-!d H:i:s; 1970-01-15 15:16:17 
Format: !d; 1970-01-15 00:00:00 

更多信息:http://in1.php.net/manual/en/datetime.createfromformat.php

0

你可以試試這個本身在MySQL

SELECT 
    DATE_FORMAT(start, '%M %D %Y') as start_formatted, 
    DATE_FORMAT(end, '%M %D %Y') as end_formatted 
FROM 
    (SELECT 
     Min(transaction_date) as start, Max(transaction_date) as end 
    from 
     transactions) t; 

mysql> SELECT DATE_FORMAT(NOW(),'%M %D %Y'); 
+-------------------------------+ 
| DATE_FORMAT(NOW(),'%M %D %Y') | 
+-------------------------------+ 
| January 27th 2014    | 
+-------------------------------+ 
1 row in set (0.00 sec) 
0

嘗試這樣

$date= $ors1['start']; 
$objdate=new DateTime($date); 
echo date_format($objdate, 'g:ia \o\n l jS F Y');