将MMDDYYYY转换为PHP的最新版本 [英] Convert MMDDYYYY to date for PHP

查看:169
本文介绍了将MMDDYYYY转换为PHP的最新版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,日期格式为MMDDYYYY(即01132012,01142012等)。



我需要在页面上执行某些操作,如果该字符串距当前日期为14天或更短。



ie。今天是01132012,所以任何带有12312011或更少日期的字符串都将在页面上显示某些东西。



有人可以帮忙吗?我试过

  echo date(d / m / Y,strtotime('01142012')); 

但无济于事。

解决方案

您可以使用PHP的DateTime类

 <? 
//当前日期
$ now = new DateTime();

//你的日期
$ date = DateTime :: createFromFormat('mdY','01142012');

//计算差异
$ diff = $ now-> diff($ date);

...

//输出格式的日期
echo $ date-> format('d / m / Y');
?>

编辑:我刚才意识到,你的格式不是php支持的。所以你必须使用alternate objectbuild。


I have a string with a date which is in this format MMDDYYYY (ie. 01132012, 01142012 etc.)

I need to do something on a page, if that string is 14 days or less from the current date.

ie. Today is 01132012, so any strings with 12312011 or a less date are going to be showing something on a page.

Can anyone help with this? I've tried

echo date("d/m/Y", strtotime('01142012'));

But to no avail.

解决方案

You can use the DateTime class of PHP

<?
  // current date
  $now = new DateTime();

  //your date
  $date = DateTime::createFromFormat('mdY', '01142012');

  // calculate difference 
  $diff = $now->diff($date);

  ...

  // output the date in format you want
  echo $date->format('d/m/Y');
?>

EDIT: I just realized, that your format isn't one supported by php. So you have to use alternate objectbuild.

这篇关于将MMDDYYYY转换为PHP的最新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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