将格式 DDMMMYY 的生日转换为 PHP 中的日期值 [英] Converting birthday in format DDMMMYY to date value in PHP

查看:19
本文介绍了将格式 DDMMMYY 的生日转换为 PHP 中的日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆以 DDMMMYY 格式存储的生日.我需要将它们转换为日期值,以便将它们存储在数据库中.

I've got bunch of birthdays which are stored in format DDMMMYY. I need to convert those to date values, so i can store those in database.

有没有什么简单的方法可以告诉 strtotime 函数日期必须是过去?

Is there any easy way of telling strtotime function that date must be in the past?

<?php
$datestring = '22SEP41';
echo date('Y-m-d',strtotime($datestring)); //outputs 2041-09-22, should be 1941-09-22
?>

推荐答案

<?php
$datestring = '22SEP41';

$matches = [];
preg_match('/([0-9]{2})([A-Z]{3})([0-9]{2})/', $datestring, $matches);

$prefix = ($matches[3] <= date('y') ? '20' : '19');

$matches[3] = "{$prefix}{$matches[3]}";

$ts = strtotime("{$matches[1]} {$matches[2]} {$matches[3]}");

// date ('Y-m-d', $ts) == 1941-09-22

这假设 22SEP06 应该被解释为 2006 而不是 1906 - 基本上它给出的输出范围是 1917 -> 2016.

This assumes that 22SEP06 should be interpreted as 2006 rather than 1906 - basically it gives the output a range of 1917 -> 2016.

这篇关于将格式 DDMMMYY 的生日转换为 PHP 中的日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆