-1

Given a date and an hour, I want to append that hour onto that date.

E.g: Given hour 3 and date 2017-05-30 I want: 2017-05-30 03:00:00

<?php

$d1 = date('Y-m-d');
$hour = 3;
$new_time = date("Y-m-d H:i:s", strtotime('+' . $hour .' hours', $d1));
echo $new_time;

?>

When running the following code I get:

Notice: A non well formed numeric value encountered on $new_time = date("Y-m-d H:i:s...

How can I get the proper output?

4 Answers4

1

try strtotime() with + hour like below :

<?php
    $d1 = date('Y-m-d');
    $new_time = date("Y-m-d H:i:s", strtotime($d1."+ 3 hour")); //here you can change the hour according to your need as now it is + 3
    echo $new_time;
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
0

You can use Datetime and add DateInterval:

$now = new DateTime(); //current date/time
$now->add(new DateInterval("PT3H"));
$new_time = $now->format('Y-m-d H:i:s');
echo $new_time;

DateInterval receives as parameter a string, if you want to learn more about the way you can format the intervals check here.

lloiacono
  • 4,714
  • 2
  • 30
  • 46
0

You can simply add the time that you need to add by strtotime($time) + $yourtimeinseconds;

Here's the Eval Link

Code :

<?php
$time = date("Y-m-d H:i:s");
$hour = 3*60*60; // To Seconds
$time = strtotime($time) + $hour; // Add 1 hour
$time = date('Y-m-d H:i:s', $time); 
echo $time;
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
0

All of the other answers are for if you want 3 hours from the current time. If you just want 3am, then just append the time, no math necessary.

$time = date('Y-m-d 03:00:00');
aynber
  • 22,380
  • 8
  • 50
  • 63