Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading