PHP - 如何从时间字符串中获取年、月、日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3605899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:23:43  来源:igfitidea点击:

PHP - How to get year, month, day from time string

php

提问by q0987

Given the following timestring:

给定以下时间字符串:

$str = '2000-11-29';

$php_date = getdate( $str );
echo '<pre>';
print_r ($php_date);
echo '</pre>';

How to get the year/month/day in PHP?

如何在 PHP 中获取年/月/日?

[seconds] => 20
[minutes] => 33
[hours] => 18
[mday] => 31
[wday] => 3
[mon] => 12
[year] => 1969
[yday] => 364
[weekday] => Wednesday
[month] => December
[0] => 2000

I don't know why I get 1969 for year.

我不知道为什么我得到 1969 年。

Thank you

谢谢

回答by Daniel Vandersluis

You can use strtotimeto parse a time string, and pass the resulting timestamp to getdate(or use dateto format your time).

您可以使用strtotime来解析时间字符串,并将生成的时间戳传递给getdate(或用于date格式化您的时间)。

$str = '2000-11-29';

if (($timestamp = strtotime($str)) !== false)
{
  $php_date = getdate($timestamp);
  // or if you want to output a date in year/month/day format:
  $date = date("Y/m/d", $timestamp); // see the date manual page for format options      
}
else
{
  echo 'invalid timestamp!';
}

Note that strtotimewill return falseif the time string is invalid or can't be parsed. When the timestamp you're trying to parse is invalid, you end up with the 1969-12-31 date you encountered before.

请注意,如果时间字符串无效或无法解析,strtotime则会返回false。当您尝试解析的时间戳无效时,您最终会得到之前遇到的 1969-12-31 日期。

回答by Pradeep Bhaskar

PHP - How to get year, month, day from time string

PHP - 如何从时间字符串中获取年、月、日

$dateValue = strtotime($q);                     

$yr = date("Y", $dateValue) ." "; 
$mon = date("m", $dateValue)." "; 
$date = date("d", $dateValue); 

回答by Raphael Caixeta

Update: Forgot to add semicolon at end of first line, try this:

更新:忘记在第一行末尾添加分号,试试这个:

<?php       
$str = "2010-08-29"; // Missed semicolon here
$time = strtotime($str);

// You can now use date() functions with $time, like
$weekday = date("l", $time); // Wednesday or whatever date it is    
?>

Hopefully that will get you going!

希望这会让你继续前进!

回答by Typhon

Using the object-oriented programming style, you can do this with DateTime class

使用面向对象的编程风格,您可以使用DateTime 类来做到这一点

$dateFormat = 'Y-m-d';
$stringDate = '2000-11-29';
$date = DateTime::createFromFormat($dateFormat, $stringDate);

Then, you can decompose your date using the format()method

然后,您可以使用format()方法分解您的日期

$year = $date->format('Y'); // returns a string

If you prefer the numeric format, instead of the string format, you can use the intval()function

如果您更喜欢数字格式而不是字符串格式,则可以使用intval()函数

$year = intval($date->format('Y')); // returns an integer

Here some formats that you can use

这里有一些你可以使用的格式

  • YA full numeric representation of a year, 4 digits
  • mMonth of the year, 2 digits with leading zeros
  • dDay of the month, 2 digits with leading zeros
  • H24-hour format of an hour, 2 digits with leading zeros
  • iMinutes, 2 digits with leading zeros
  • sSeconds, 2 digits with leading zeros
  • Y一年的全数字表示,4 位数字
  • m一年中的月份,带前导零的 2 位数字
  • d月份中的第几天,带前导零的 2 位数字
  • H小时的 24 小时制,2 位数字,带前导零
  • i分钟,带前导零的 2 位数字
  • s秒,带前导零的 2 位数字

Here the entire list of the formats that you can use : http://php.net/manual/en/function.date.php

这里是您可以使用的格式的完整列表:http: //php.net/manual/en/function.date.php