在PHP中将字符串转换为日期

时间:2012-10-17 08:51:32

标签: php date

首先我在上一页输入e6 = 24-09-2011作为输入类型=“文本”:

  $a6 = $_POST["e6"]   ; 
  $time = strtotime( $a6 );
  $myDate = date ("y-m-d", $time ); 
  echo $myDate ;
  $n = strtotime(date("Y-m-d", strtotime($myDate)) . " +$a7 month");
  $q = date("Y-m-d", $n);
  echo $q ;

out put: 11-09-24 2013-09-24

我想打印2011而不是11。我该怎么办?请帮助。

2 个答案:

答案 0 :(得分:3)

 $a6 = $_POST["e6"]   ; 
    $time = strtotime( $a6 );
    $myDate = date ("Y-m-d", $time ); 
    echo $myDate ;
    $n = strtotime(date("Y-m-d", strtotime($myDate)) . " +$a7 month");
    $q = date("Y-m-d", $n);
    echo $q ;

您需要将所有小写字母y更改为大写字母Y See here

答案 1 :(得分:0)

$myDate = date ("y-m-d", $time );
应该像资本'Y'一样:
$myDate = date ("Y-m-d", $time );

Php docs对数据字符串格式有一个非常好的解释:Here