2012-03-03 30 views

回答

5

如果你没有调用日期和时间函数,不按你的问题它做到这一点,好像你只需要字符串操作。使用sscanf()

list($date,$month,$year) = sscanf("01/01/2012", "%d/%d/%d"); 
echo "$month-$date-$year"; 
+1

哈,非常好:P – Halcyon 2012-03-03 23:22:35

+0

如果你想在你的日期和月份领先一个零,这里有一个网站解释如何。 https://bugs.php.net/bug.php?id=7705 以下是我如何使用上述和链接... 'list($ date,$ month,$ year)= sscanf(“01/01/2012“,”%02s /%02s /%04s“);' 'echo”$ year- $ month- $ date“;' – MarkNHopgood 2016-04-07 08:41:53

-1
<?php 
$format1 = strtotime("01/01/2012"); 
//$format2 = strtotime("2012-01-01"); 
echo date("Y-m-d",$format1); 
?> 
+2

当使用斜杠时,strtotime()会采用'mm/dd/yyyy',这不是英式方法,所以这会导致“2012年4月3日”错误结果(2012年3月4日),因为“strtotime”会读到4月3日。 – 2012-03-03 23:23:59

3

您可以使用strtotime但这是一个神奇的功能,所以我不信任它。

一种更谨慎的做法是刚做了非常手动转换,如:

$us_date = "01/01/2012"; 
$parts = explode("/", $us_date, 3); 
$uk_date = $parts[2] . "-" . $parts[0] . "-" . $parts[1]; // flip day and month? 
+0

有趣!您可能想要切换$ us_date和$ uk_date变量tho! – methuselah 2012-03-03 23:34:05