从日期字符串获取月,日,年的最快方法

时间:2022-09-27 20:28:00

I am getting a date from the mysql database:

我从mysql数据库得到一个日期:

here is what it comes out as:

这是它的结果:

2017-01-20

what would be the fastest way to get the month, day, and year, so for example, when i echo, it will be like this:

什么是获得月,日和年的最快方式,例如,当我回声时,它将是这样的:

echo $month; //01
echo $day; //20
echo $year; //2017

4 个解决方案

#1


1  

Well, if you know that the output will consistently be a string in the format "YYYY-MM-DD", the most basic approach is:

好吧,如果您知道输出将始终是格式为“YYYY-MM-DD”的字符串,则最基本的方法是:

<?php

    $query = ... //query is your string "YYYY-MM-DD"
    $year = substr($query, 0, 4);
    $month = substr($query, 5, 2);
    $day = substr($query, 8, 2);

    echo $month;
    echo $day;
    echo $year;
?>

#2


1  

Let's assume you have that string in a variable called $date

假设您在名为$ date的变量中包含该字符串

$date = '2017-01-20';

You can explode it into a list if you are sure the format is consistent:

如果您确定格式一致,可以将其分解为列表:

list($year, $month, $day) = explode("-", $date, 3);

You could convert the date to a time integer using strtotime to use in other functions like date. This has the added benefit of being able to test that this is a well-formed date:

您可以使用strtotime将日期转换为时间整数,以便在日期等其他函数中使用。这样做的另一个好处是能够测试这是一个结构良好的日期:

$time = strtotime($date);
if ($time === false) die("Bad date format: $date.");
$year = date('Y', $time);
$month = date('m', $time); // 'n' if you don't want leading zero
$day = date('d', $time);   // 'j' if you don't want leading zero

As jasonmoqio points out, since you asked for fastest, substr is a tiny bit faster than exploding. (On my workstation looping substr vs. explode 10 million times only produced an improvement of 1/1000th of a second over exploding, so unless this is in a loop that gets run millions of times, you will not notice the difference and should opt for code readability.)

正如jasonmoqio指出的那样,因为你要求最快,所以substr比爆炸要快一点。 (在我的工作站上循环substr与爆炸1000万次只比爆炸产生了1/1000秒的改进,所以除非这是一个循环运行数百万次,你不会注意到差异,应该选择代码可读性。)

$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);

#3


0  

Try this:

$date = new DateTime('2017-01-20');
echo 'Year:'.$date->format("Y");
echo 'Month:'.$date->format("m");
echo 'Day:'.$date->format("d");

Output:

Year:  2017
Month: 01
Day:   20

#4


0  

If you want to quickly get the date from mysql, try using regex like this.

如果你想从mysql快速获取日期,请尝试使用这样的正则表达式。

if (preg_match('/^(?P<year>\d+)[-\/](?P<month>\d+)[-\/](?P<day>\d+)$/', $your_date, $matches)) {
    $mydate = $matches['year'] . "-" . $matches['month'] . "-" . $matches['day'];
    $whatever = date('Y-m-d', strtotime($tgl));
    // You can echo it...
    // echo $matches['year'];
    // echo $matches['month'];
    // echo $matches['day'];
}

Hope this help you out. :D

希望这可以帮到你。 :d

#1


1  

Well, if you know that the output will consistently be a string in the format "YYYY-MM-DD", the most basic approach is:

好吧,如果您知道输出将始终是格式为“YYYY-MM-DD”的字符串,则最基本的方法是:

<?php

    $query = ... //query is your string "YYYY-MM-DD"
    $year = substr($query, 0, 4);
    $month = substr($query, 5, 2);
    $day = substr($query, 8, 2);

    echo $month;
    echo $day;
    echo $year;
?>

#2


1  

Let's assume you have that string in a variable called $date

假设您在名为$ date的变量中包含该字符串

$date = '2017-01-20';

You can explode it into a list if you are sure the format is consistent:

如果您确定格式一致,可以将其分解为列表:

list($year, $month, $day) = explode("-", $date, 3);

You could convert the date to a time integer using strtotime to use in other functions like date. This has the added benefit of being able to test that this is a well-formed date:

您可以使用strtotime将日期转换为时间整数,以便在日期等其他函数中使用。这样做的另一个好处是能够测试这是一个结构良好的日期:

$time = strtotime($date);
if ($time === false) die("Bad date format: $date.");
$year = date('Y', $time);
$month = date('m', $time); // 'n' if you don't want leading zero
$day = date('d', $time);   // 'j' if you don't want leading zero

As jasonmoqio points out, since you asked for fastest, substr is a tiny bit faster than exploding. (On my workstation looping substr vs. explode 10 million times only produced an improvement of 1/1000th of a second over exploding, so unless this is in a loop that gets run millions of times, you will not notice the difference and should opt for code readability.)

正如jasonmoqio指出的那样,因为你要求最快,所以substr比爆炸要快一点。 (在我的工作站上循环substr与爆炸1000万次只比爆炸产生了1/1000秒的改进,所以除非这是一个循环运行数百万次,你不会注意到差异,应该选择代码可读性。)

$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);

#3


0  

Try this:

$date = new DateTime('2017-01-20');
echo 'Year:'.$date->format("Y");
echo 'Month:'.$date->format("m");
echo 'Day:'.$date->format("d");

Output:

Year:  2017
Month: 01
Day:   20

#4


0  

If you want to quickly get the date from mysql, try using regex like this.

如果你想从mysql快速获取日期,请尝试使用这样的正则表达式。

if (preg_match('/^(?P<year>\d+)[-\/](?P<month>\d+)[-\/](?P<day>\d+)$/', $your_date, $matches)) {
    $mydate = $matches['year'] . "-" . $matches['month'] . "-" . $matches['day'];
    $whatever = date('Y-m-d', strtotime($tgl));
    // You can echo it...
    // echo $matches['year'];
    // echo $matches['month'];
    // echo $matches['day'];
}

Hope this help you out. :D

希望这可以帮到你。 :d