获取日期格式,如“Y-m-d H:i:s”,从php日期开始。

时间:2021-11-14 21:29:49

Does someone know a way to get a string from a 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 written, usually to get the date with a different timezone from a 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

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

4 个解决方案

#1


5  

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.

就我所知,没有保证的工作方式。最好的方法可能是尝试将正则表达式与预期的知名格式(例如\d{2,4}[-/]\d{2}{2})匹配,但我想不出一个简单的方法来进行匹配,而不用使用正则表达式。您还需要检查解析格式是否有意义,并且您不能在诸如2009年3月2日这样的模糊日期上做很多事情,这可能会被表示为09/03/02,2009-03-02,02/03/09,03/02/09,甚至09/02/03。

#2


9  

You can convert string date to timestamp with strtotime and do with the timestamp what ever you want. ;)

您可以使用strtotime将字符串日期转换为时间戳,并使用时间戳处理您想要的时间戳。,)

<?php
$date = "2009-10-16 21:30:45";
$ts   = strtotime($date);
echo date('Y-m-d', $ts);
?>

#3


4  

I know it's late, but would it be best to do:

我知道已经很晚了,但是最好是这样做:

$date = date("Y-m-d");

#4


3  

The PHP date() function formats a timestamp to a more readable date and time. Try this code for you project,

函数的作用是:将时间戳格式化为更可读的日期和时间。为您的项目尝试这段代码,

 <?php
    $date=date_create('2014-10-20');
    date_time_set($date,12,20,55);
    echo date_format($date,'Y-m-d H:i:s');
    ?>

#1


5  

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.

就我所知,没有保证的工作方式。最好的方法可能是尝试将正则表达式与预期的知名格式(例如\d{2,4}[-/]\d{2}{2})匹配,但我想不出一个简单的方法来进行匹配,而不用使用正则表达式。您还需要检查解析格式是否有意义,并且您不能在诸如2009年3月2日这样的模糊日期上做很多事情,这可能会被表示为09/03/02,2009-03-02,02/03/09,03/02/09,甚至09/02/03。

#2


9  

You can convert string date to timestamp with strtotime and do with the timestamp what ever you want. ;)

您可以使用strtotime将字符串日期转换为时间戳,并使用时间戳处理您想要的时间戳。,)

<?php
$date = "2009-10-16 21:30:45";
$ts   = strtotime($date);
echo date('Y-m-d', $ts);
?>

#3


4  

I know it's late, but would it be best to do:

我知道已经很晚了,但是最好是这样做:

$date = date("Y-m-d");

#4


3  

The PHP date() function formats a timestamp to a more readable date and time. Try this code for you project,

函数的作用是:将时间戳格式化为更可读的日期和时间。为您的项目尝试这段代码,

 <?php
    $date=date_create('2014-10-20');
    date_time_set($date,12,20,55);
    echo date_format($date,'Y-m-d H:i:s');
    ?>