PHP 将 2 位数年份转换为 4 位数年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17030366/
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
PHP convert 2 digit year to a 4 digit year
提问by user2327201
I have data coming from the database in a 2 digit year format 13
I am looking to convert this to 2013
I tried the following code below...
我有来自数据库的 2 位数年份格式的数据13
我希望将其转换为2013
我尝试了以下代码...
$result = '13';
$year = date("Y", strtotime($result));
But it returned 1969
但它回到了 1969 年
How can I fix this?
我怎样才能解决这个问题?
回答by webbiedave
$dt = DateTime::createFromFormat('y', '13');
echo $dt->format('Y'); // output: 2013
69
will result in 2069
. 70
will result in 1970
. If you're ok with such a rule then leave as is, otherwise, prepend your own century data according to your own rule.
69
将导致2069
. 70
将导致1970
. 如果您同意这样的规则,则保持原样,否则,根据您自己的规则添加您自己的世纪数据。
回答by Chris Throup
One important piece of information you haven't included is: how do you think a 2-digit year should be converted to a 4-digit year?
您未包含的一项重要信息是:您认为如何将 2 位数年份转换为 4 位数年份?
For example, I'm guessing you believe 01/01/13 is in 2013. What about 01/01/23? Is that 2023? Or 1923? Or even 1623?
例如,我猜你认为 01/01/13 是在 2013 年。那么 01/01/23 呢?那是2023年吗?还是1923年?甚至1623?
Most implementations will choose a 100-year period and assume the 2-digits refer to a year within that period.
大多数实现将选择 100 年的时间段,并假设 2 位数字表示该时间段内的一年。
Simplest example: year is in range 2000-2099.
最简单的例子:年份在 2000-2099 范围内。
// $shortyear is guaranteed to be in range 00-99
$year = 2000 + $shortyear;
What if we want a different range?
如果我们想要一个不同的范围怎么办?
$baseyear = 1963; // range is 1963-2062
// this is, of course, years of Doctor Who!
$shortyear = 81;
$year = 100 + $baseyear + ($shortyear - $baseyear) % 100;
Try it out. This uses the modulo function (the bit with %) to calculate the offset from your base year.
试试看。这使用模函数(带有 % 的位)来计算与基准年的偏移量。
回答by Igor S.
$result = '13';
$year = '20'.$result;
if($year > date('Y')) {
$year = $year - 100;
}
//80 will be changed to 1980
//12 -> 2012
回答by JonnyS
The issue is with strtotime. Try the same thing with strtotime("now").
问题在于strtotime。用 strtotime("now") 尝试同样的事情。
回答by IanPudney
Simply prepend (add to the front) the string "20" manually:
只需手动添加(添加到前面)字符串“20”:
$result = '13';
$year = "20".$result;
echo $year; //returns 2013
回答by Praveen Kumar Purushothaman
This might be dumbest, but a quick fix would be:
这可能是最愚蠢的,但快速解决方法是:
$result = '13';
$result = '1/1/20' . $result;
$year = date("Y", strtotime($result)); // Returns 2013
Or you can use something like this:
或者你可以使用这样的东西:
date_create_from_format('y', $result);
回答by Schleis
You can create a date object given a format with date_create_from_format()
您可以创建一个给定格式的日期对象 date_create_from_format()
http://www.php.net/manual/en/datetime.createfromformat.php
http://www.php.net/manual/en/datetime.createfromformat.php
$year = date_create_from_format('y', $result);
echo $year->format('Y')
回答by hek2mgl
Use the DateTime
class, especially DateTime::createFromFormat()
, for this:
为此使用DateTime
类,尤其是DateTime::createFromFormat()
:
$result = '13';
// parsing the year as year in YY format
$dt = DateTime::createFromFormat('y', $result);
// echo it in YYYY format
echo $dt->format('Y');
回答by Jim Raymond
I'm just a newbie hack and I know this code is quite long. I stumbled across your question when I was looking for a solution to my problem. I'm entering data into an HTML form (too lazy to type the 4 digit year) and then writing to a DB and I (for reasons I won't bore you with) want to store the date in a 4 digit year format. Just the reverse of your issue.
我只是一个新手黑客,我知道这段代码很长。我在寻找解决问题的方法时偶然发现了您的问题。我正在将数据输入到 HTML 表单中(懒得输入 4 位数年份),然后写入数据库,我(因为我不会让您感到厌烦)想以 4 位数年份格式存储日期。刚好与你的问题相反。
The form returns $date (I know I shouldn't use that word but I did) as 01/01/01. I determine the current year ($yn) and compare it. No matter what year entered is if the date is this century it will become 20XX. But if it's less than 100 (this century) like 89 it will come out 1989. And it will continue to work in the future as the year changes. Always good for 100 years. Hope this helps you.
表单返回 $date (我知道我不应该使用那个词,但我使用了)作为 01/01/01。我确定当前年份 ($yn) 并进行比较。无论输入的是哪一年,如果日期是本世纪,它将变成 20XX。但是如果它小于 100(本世纪),比如 89,它会在 1989 年出来。而且它会随着年份的变化在未来继续工作。100 年总是好的。希望这对你有帮助。
// break $date into two strings
// 将 $date 分成两个字符串
$datebegin = substr($date, 0,6);
$dateend = substr($date, 6,2);
// get last two digits of current year
// 获取当前年份的最后两位数
$yn=date("y");
// determine century
//确定世纪
if ($dateend > $yn && $dateend < 100)
{
$year2=19;
}
elseif ($dateend <= $yn)
{
$year2=20;
}
// bring both strings back into one
// 将两个字符串合二为一
$date = $datebegin . $year2 . $dateend;
回答by julius patta
I had similar issues importing excel (CSV) DOB fields, with antiquated n.american style date format with 2 digit year. I needed to write proper yyyy-mm-dd to the db. while not perfect, this is what I did:
我在导入 excel (CSV) DOB 字段时遇到了类似的问题,使用过时的美国风格日期格式和 2 位数年份。我需要将正确的 yyyy-mm-dd 写入数据库。虽然不完美,但这就是我所做的:
//$col contains the old date stamp with 2 digit year such as 2/10/66 or 5/18/00
$yr = \DateTime::createFromFormat('m/d/y', $col)->format('Y');
if ($yr > date('Y')) $yr = $yr - 100;
$md = \DateTime::createFromFormat('m/d/y', $col)->format('m-d');
$col = $yr . "-" . $md;
//$col now contains a new date stamp, 1966-2-10, or 2000-5-18 resp.