確實有人知道從日期獲取包含日期格式的字符串的方式嗎?從php日期獲取日期格式如「Y-m-d H:i:s」
<?php
$date = date ("2009-10-16 21:30:45");
// smething like this?
print date_format ($date);
?>
我問這個,因爲我想優化這個功能,我寫的,通常獲取日期與服務器不同的時區,而不會做特別的事情
<?php
function get_timezone_offset ($timezone, $date = null, $format = null, $offset_timezone = null) {
if ($date == null) $date = date ($format);
if ($offset_timezone == null) $offset_timezone = date_default_timezone_get();
if ($format == null) $format = "Y-m-d H:i:s";
// I'd like to find a way that can avoid me to write $format and get it directly from the date i pass, but I don't know a particular method can do it
// if ($format == null) $format = date_format ($date);
$date_time = new DateTime ($date, new DateTimeZone ($offset_timezone));
$date_time->setTimeZone (new DateTimeZone ($timezone));
return $date_time->format ($format);
}
print get_timezone_offset ("Europe/Rome");
print get_timezone_offset ("Europe/Rome", date ("Y-m-d H:i:s"));
print get_timezone_offset ("Europe/Rome", date ("Y-m-d H:i:s"), "Y-m-d H:i:s");
print get_timezone_offset ("Europe/Rome", "2009-10-16 21:30:45", "Y-m-d H:i:s", "America/New_York");
?>
我希望避免正則表達式的性能原因,但我不知道這是否可能
唯一一個真正理解OP的問題的人! – 2015-12-14 20:42:58