abhi
abhi

Reputation: 9

converting string to date in php

First i entered e6 = 24-09-2011 as input type = "text" in previous page then:

  $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

I want to print 2011 in place of only 11. What should I do ?? pls help.

Upvotes: 0

Views: 121

Answers (2)

Flame
Flame

Reputation: 363

$myDate = date ("y-m-d", $time );
should be with a capital 'Y' like so :
$myDate = date ("Y-m-d", $time );

Php docs has a very good explanation on the data-string formatting: Here

Upvotes: 0

RSM
RSM

Reputation: 15108

 $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 ;

You needed to change all the lower case y to capital Y. See here.

Upvotes: 3

Related Questions