I am trying to add the time to the date but I am having trouble with the correct code to do it.
$dateToday = date('Y-m-d');
$timeToAddToDate = 2:52; // This is from user input so it is not always 2:52 am o'clock, sometimes 21:30 o'clock depending on the user input;
I tried
$s = new DateTime($date_now);
$t = $s->modify('+{$timeToAddToDate} seconds');
$results = date('Y-m-d H:i:s',$t); // this is not working`
echo $results;`
I am expecting the results to be "2024-07-28 2:52" or "{date today} {time input by user}".
I want the results then converted to timestamp and save it to the database column with a timestamp type.
So after the $results, I will convert it to strtotime($results) then insert it via sql query to the database.
2
Answers
For this you can use strtotime:
$dateToday = date("Y-m-d"); $resultTimestamp = strtotime($today . " ". $timeToAddToDate); $resultDate = date("Y-m-d H:i", $resultTimestamp);
This is a way to go (you can compact the code, I probably overdetailed it.)