English 中文(简体)
PHP [复制]
原标题:Convert MMDDYYYY to date for PHP [duplicate]

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

我需要在一页上做一些事情,如果从现在起到14天左右。

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

谁能帮助? 我受审

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

但没有结果。

最佳回答

您可以使用购买力平价的时段。

<?
  // 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:我刚刚认识到,你的格式没有一种得到php支持的形式。 因此,你必须使用替代物体。

问题回答

我更喜欢使用平时。

<?
$dt = strptime( 01142012 ,  %m%d%Y );
echo sprintf("%02d/%02d/%04d", $dt[ tm_mday ], $dt[ tm_mon ]+1, $dt[ tm_year ]+1900);

如果你使用PHP5.3或以上,你也可以使用日期_parse_ from_format()

有多少次子午餐?

$string =  01142012 ;
$time = mktime(0, 0, 0, 
          substr($string, 0, 2),
          substr($string, 2, 2),
          substr($string, 4, 4)
        );
 echo date( d/m/Y , $time);
try date( m-d-y , strtotime( 01142012 ));

也可以尝试类似的东西;

$var = strtotime( 01142012 );
$var2 = date ( F j, Y , $var);

你提出的1 0142012年建议不能用时间加以限定,因为作为答案,这并不有效。 为了把这个日期改为一个有效日期,你需要添加斜体或 da,以区分数字。

最容易的方式是将日期与浮标或斜).相储存,例如从现在起在数据库中储存01-14-2012年或01/14/2012年,或者你将不得不自行创建功能,将数字转换成实时的有效形式。

你们可以这样做:

function makeValidDate($date) {

   $valid_date = array();
   $array = str_split($date); //split characters up

   foreach($array as $key => $character){
       if($key==2 || $key==4){
           $character =  - .$character; //add relevant formatting to date
           $valid_date[] = $character; //add this to the formatted array
       }
       else{
           $valid_date[] = $character; // if not dashes or slashes needed add to valid array
       }
   }

   return implode($valid_date); // return the formmatted date for use with strtotime
}

届时,你可以做到:

$valid_date = makeValidDate( 01142012 );    
echo date("d/m/Y", strtotime($valid_date));

I haven t tested this but you should get a good idea of what to do.

EDIT: Capi s idea is a lot cleaner!!

try "preg_match(pattern,string on wich the pattern will be aplied)"; http://www.php.net/manual/en/function.preg-match.php you can also define an offset. so first take te first 2 digits. than take the other 2 digits and after that get the other four digits. after that place them in one string. after that use maketime,strtotime,date. this kind of stupid solution but i only thought of that. hope this will help





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签