从指定日期获取年份 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4529640/
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 13:20:14  来源:igfitidea点击:

Get the year from specified date php

phpdate

提问by Shakti Singh

I have a date in this format 2068-06-15. I want to get the year from the date, using php functions. Could someone please suggest how this could be done.

我有一个这种格式的日期2068-06-15。我想使用 php 函数从日期中获取年份。有人可以建议如何做到这一点。

回答by Maerlyn

$date = DateTime::createFromFormat("Y-m-d", "2068-06-15");
echo $date->format("Y");

The DateTime class does not use an unix timestamp internally, so it han handle dates before 1970 or after 2038.

DateTime 类在内部不使用 unix 时间戳,因此它可以处理 1970 年之前或 2038 年之后的日期。

回答by Sarfraz

You can use the strtotimeand datefunctions like this:

您可以像这样使用strtotimedate函数:

echo date('Y', strtotime('2068-06-15'));

Note however that PHP can handle year upto 2038

但是请注意,PHP 可以处理到 2038 年

You can test it out here

你可以在这里测试



If your date is always in that format, you can also get the year like this:

如果您的日期始终采用这种格式,您还可以像这样获得年份:

$parts = explode('-', '2068-06-15');
echo $parts[0];

回答by carnini

I would use this:

我会用这个:

$parts = explode('-', '2068-06-15');
echo $parts[0];

It appears the date is coming from a source where it is always the same, much quicker this way using explode.

日期似乎来自一个始终相同的来源,使用爆炸这种方式要快得多。

回答by Hiba

  public function getYear($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("Y");
}

public function getMonth($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("m");
}

public function getDay($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("d");
}

回答by fvox

<?php
list($year) = explode("-", "2068-06-15");
echo $year;
?>

回答by RashedRahat

You can achieve your goal by using php date() & explode() functions:

您可以通过使用 php date() 和 purge() 函数来实现您的目标:

$date = date("2068-06-15");

$date = date("2068-06-15");

$date_arr = explode("-", $date);

$date_arr = explode("-", $date);

$yr = $date_arr[0];

$yr = $date_arr[0];

echo $yr;

echo $yr;

That is it. Happy coding :)

这就对了。快乐编码:)

回答by bidon

You wrote that format can change from YYYY-mm-dd to dd-mm-YYYY you can try to find year there

你写的格式可以从 YYYY-mm-dd 更改为 dd-mm-YYYY 你可以尝试在那里找到年份

$parts = explode("-","2068-06-15");
for ($i = 0; $i < count($parts); $i++)
{
     if(strlen($parts[$i]) == 4)
     {
          $year = $parts[$i];
          break;
      }
  }

回答by Vineesh Kalarickal

$Y_date = split("-","2068-06-15");
$year = $Y_date[0];

You can use explode also

您也可以使用爆炸

回答by dshipper

Assuming you have the date as a string (sorry it was unclear from your question if that is the case) could split the string on the - characters like so:

假设您将日期作为字符串(很抱歉,您的问题不清楚是否是这种情况)可以将字符串拆分为 - 字符,如下所示:

$date = "2068-06-15";
$split_date = split("-", $date);
$year = $split_date[0];