获取日期格式,如“Y-m-d H:i:s”从php日期 [英] Get date format like "Y-m-d H:i:s" from a php date

查看:207
本文介绍了获取日期格式,如“Y-m-d H:i:s”从php日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道有什么办法从包含日期格式的日期获取字符串?

 <?php 
$ date = date(2009-10-16 21:30:45);

//这样吗?
打印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;
//我想找到一种方法,可以避免我写$格式,直接从我通过的日期获取它,但我不知道一个特定的方法可以做到
// 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);
?>

我希望避免出于性能原因的正则表达式,但我不知道这是否可能

解决方案

据我所知,没有保证的工作方式。最好的方法可能是尝试将正则表达式与预期的知名格式相匹配(例如 \d {2,4} [ - /] \d {2} [ - /] \d {2} 为Ymd),但我不能想到一个简单的方法来做匹配而不使用正则表达式。您还必须检查解析的格式是否有意义,并且您不能对2009年3月2日之前的模糊日期做许多事情,可以表示为 09/03/02 2009-03-02 02/03/09 03/02 / 09 ,甚至 09/02/03


did someone knows a way to get a string from date that contains the format of the date?

<?php
    $date = date ("2009-10-16 21:30:45");

    // smething like this?
    print date_format ($date);
?>

I ask this because I'd like to optimize this function I've wrote, usual to get date with a different timezone from server, without doing particular things

<?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");
?>

I hope to avoid regular expressions for performance reasons, but I don't know if this is possible

解决方案

As far as I'm aware, there's no guaranteed way of working backwards. The best way might be to try to match regular expressions against expected well-known formats (e.g. \d{2,4}[-/]\d{2}[-/]\d{2} for "Y-m-d") but I can't think of an easy way to do the matching without using regular expressions. You would also have to check that the parsed format makes sense, and you can't do much about ambiguous dates like 2nd of March 2009, which could be represented as 09/03/02, 2009-03-02, 02/03/09, 03/02/09, or even 09/02/03.

这篇关于获取日期格式,如“Y-m-d H:i:s”从php日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆