2

I need add extra 24 hours to my default time and store in database like start time is = default time, end time is = start time + 24 hours.

I already try this way. hear is the code.

<?php
date_default_timezone_set("Asia/Colombo");

$time = date("H:i:s");

$validtime = '24:00:00';

$endtime = strtotime($time + $validtime);

echo date('H:i:s', $endtime);

$bookname= $_GET['id'];


$link=mysqli_query($conn, "update reserve_table set username='$_SESSION[username]', bookname='$bookname', reservetime='$time', endtime='$endtime'");
?>

and pop up this error

Notice: A non well formed numeric value encountered in /opt/lampp/htdocs/Lowa/student/reserve.php on line 33

Notice: A non well formed numeric value encountered in /opt/lampp/htdocs/Lowa/student/reserve.php on line 33 05:30:00

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • Please update your question to include code. – ascsoftw Jul 21 '19 at 05:47
  • You are adding 24 hours to time without a date, it will always be same time. Unless you are storing $endTime as DateTime it doesn't make any sense. – ascsoftw Jul 21 '19 at 05:52
  • See [DateTime::add](https://php.net/manual/en/datetime.add.php). For example, [Time calculation in php](https://stackoverflow.com/a/1665723/924299). – showdev Jul 21 '19 at 08:28
  • You wouldn't try to do maths with strings (`$sum = 'one' + 'three'`), would you? I suggest you don't do the same with dates. – Álvaro González Jul 21 '19 at 08:42

1 Answers1

1

PHP cannot add dates in the '24:00:00' format. This is a strings, not a number or "time" thingy. You can add time when it is expressed as a number in seconds. So this will work:

// get time in seconds as an integer
$time = time();
// show it in seconds and formated
echo "In seconds: $time formated: " . date("Y-m-d H:i:s", $time);
// add a day
echo "<br>Add a day.<br>";
$time += 24 * 60 * 60;
// show it in seconds and formated
echo "In seconds: $time formated: " . date("Y-m-d H:i:s", $time);

time() return the current time in seconds. We then add a whole day worth of seconds and convert it to the format you need.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • still that problem. can you help me to write the code for add 24 hours to current time. –  Jul 21 '19 at 06:07
  • @TharukaDananjaya It is not clear what problem you're now talking about. I've updated my question, to clearly show that the time has advanced one day. – KIKO Software Jul 21 '19 at 06:14