Failing to format my date to work correctly in php

Working on a PHP form that sends date I create a time/date from a input form. (jquery datepicker)

$dateinput = $_POST['datepicker'];
$t1 = date_create($dateinput);
//times are posted seperate and stipped from a input box (either 1055 or 10:55)
$t1_hour = substr($t1_raw,0,2);
$t1_min = substr($t1_raw,-2);

$t1->add(new DateInterval('PT' . $t1_hour . 'H'));
$t1->add(new DateInterval('PT' . $t1_min . 'M'));

print_r($t1);

This all seems to work

DateTime Object ( [date] => 2023-09-04 10:55:00.000000 [timezone_type] => 3 [timezone] => Europe/Amsterdam )

But when I try to pull the day of the yaer it fails to work with this data and returns nothing.

echo date('z',$t1);

I can print the information

$datetime_1 = date_format($t1, 'm-d-Y H:i'); 
echo $datetime_1;

Also works and returns: 09-04-2023 10:55

echo('z',$datetime_1);

returns 0

Where am I overlooking something?

print_r(getDate($datetime_1));

somehow is completely broken

Array ( 
[seconds] => 9 
[minutes] => 0 
[hours] => 1 
[mday] => 1 
[wday] => 4 
[mon] => 1 
[year] => 1970 
[yday] => 0 
[weekday] => Thursday 
[month] => January 
[0] => 9 
)

>Solution :

$dateinput = $_POST['datepicker'];
$t1 = date_create($dateinput);
//times are posted seperate and stipped from a input box (either 1055 or 10:55)
$t1_hour = substr($t1_raw,0,2);
$t1_min = substr($t1_raw,-2);

$t1->add(new DateInterval('PT' . $t1_hour . 'H'));
$t1->add(new DateInterval('PT' . $t1_min . 'M'));

print_r($t1);

// HERE: Use the format method of the DateTime object to get the day of the year
echo $t1->format('z');

Leave a Reply