1

I want to get day, month, and year from a date but it doesn't work. I always get an error:

Fatal error: Call to a member function format() on a non-object in printtest.php on line 4

This is my printtest.php code. I got an error. How to fix this? Check live demo here.

<?php
    $dob = $_POST['dob'];
    $date = DateTime::createFromFormat("Y-m-d", '$dob');
    $year1 = $date->format("Y");
    $day1 = $date->format("d");
    $mon3 = $date->format("m");
    echo $year1;
?>
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Savan Paun
  • 1,723
  • 2
  • 16
  • 24
  • 2
    `createFromFormat("Y-m-d", '$dob')` — You're using single-quotes. Variables do not get interpolated when they're in single-quotes. Drop the quotes from there (or use double-quotes instead). – Amal Murali Jun 04 '14 at 06:21
  • 1
    i added double quotes but not working getting same error :( – Savan Paun Jun 04 '14 at 06:23
  • 2
    The datepicker sends the date in `m-d-Y` format. You're using `Y-m-d` instead. Change it to use `m-d-Y`: `$date = DateTime::createFromFormat("m-d-Y", $dob);`. – Amal Murali Jun 04 '14 at 06:29
  • 1
    Dear @AmalMurali why you marked my question as duplicated ?? this question and datepicker is not same check – Savan Paun Jun 04 '14 at 06:37
  • @user3705511 I dont think this is a duplicate. You had a format error as well as the quoted variable error. Take a look at my answer. – Dan Sherwin Jun 04 '14 at 06:39
  • @NullPoiиteя The linked duplicate does not address the problem here. It only talks about the broad "member function ... on non-object" error message, which is very vague and not of any particular use to the OP (or anyone else reading the question). – Amal Murali Jun 04 '14 at 06:41
  • @DanSherwin my question have both error quoted and fatal error so both question is no duplicated check the DateTime::createFromFormat("m-d-Y", $dob); this side error solved – Savan Paun Jun 04 '14 at 06:42
  • 1
    @NullPoiиteя you maked my question as duplicate without read or tested it's not fair in stackoverflow web..check the website faculty and rules thank you – Savan Paun Jun 04 '14 at 06:44
  • And the change is to `d-m-Y` and not `m-d-Y`. Look at sample site. Day is before the month. – Dan Sherwin Jun 04 '14 at 06:45
  • yes @DanSherwin my problem already solved by amal murali's answer :) anyway thak you – Savan Paun Jun 04 '14 at 06:46

3 Answers3

0

Remove the single quotes around your dob variable as Alessandro said, but also, according to your sample site, the input field is in the format d-m-Y, and the format you are passing is Y-m-d.

So change

$date = DateTime::createFromFormat("Y-m-d", '$dob');

to be

$date = DateTime::createFromFormat("d-m-Y", $dob);
Dan Sherwin
  • 695
  • 3
  • 14
0
Try the below code to get the Year

<?php
    $dob    = $_POST['dob'];
    $date   = explode("-",$dob);
    $year1  = $date[2];
    echo $year1;
?> 
S Vinesh
  • 529
  • 1
  • 4
  • 15
-1

Variable interpolation only happens in double-quoted strings so this code is attempeting to parse the string "$dob" as a Y-m-d date, rather than the string contained in the variable $dob. This can be fixed by changing DateTime::createFromFormat("Y-m-d", '$dob') to DateTime::createFromFormat("Y-m-d", "$dob")

Also, since you may not have control over the values posted to your script you should check that DateTime::createFromFormat() returned an object (it returns false on failure) before attempting to operate on its return value, e.g.

$date = DateTime::createFromFormat("Y-m-d", "$dob");
if (!$date)
{
   echo "Invalid dob (YYYY-mm-dd): $dob";
   exit;
}

$year1 = $date->format("Y");
$day1 = $date->format("d");
$mon3 = $date->format("m");
echo $year1;

This way if a dob that can't be parsed as a Y-m-d date is posted you won't get the same fatal error but rather a more useful "Invalid dob" message.

FoolishSeth
  • 3,953
  • 2
  • 19
  • 28