-1

I need to create a date object that will be changed by parameters that i get. If I'll get -7 for days it will take me a week ago.

Here is my code. How do I format the date correctly?

 public function get_time_get($myear=0,$mmonth=0,$mday=0,$mhour=0,$mminute=0,$msecond=0){
    $year=date('y') +$myear;
    $month=date('m')+$mmonth;
    $day = date('d')+$mday;
    $hour= date('H'+$mhour); // there is a bug 
    $minute = date('i')+$mminute;
    $seconds= date('s')+$msecond;

    $date=mktime($year,$month,$day,$hour,$minute,$seconds);
    $t =date("Y-m-d H:i:s", $date);
    debug($date);

}

You can see that I try to get the time , but I get this:2021-11-30 17:08:29 That is not correct

matisa
  • 497
  • 3
  • 25
  • Can you show us how you call the function and what output you get vs expect? I would suggest using a `DateTime` object which would simply allow you to do stuff like `$date->modify('+2 hours');` http://php.net/manual/en/datetime.modify.php – Giedrius Aug 29 '17 at 16:04
  • 1
    Take a look at the documentation of [`mktime()`](http://php.net/manual/en/function.mktime.php) (it expects the date&time components in a different order than you might think) then forget about it completely. Try to use the [`DateTime`](http://php.net/manual/en/class.datetime.php) class and its friends. They are easier to use than the old `mktime()`/`date()` pair, they produce the same outcome and they can work with multiple timezones (while the old date & time functions either don't know anything about the timezone or they use only the default one.) – axiac Aug 29 '17 at 16:07

1 Answers1

3

You wrote:

$hour= date('H'+$mhour);

But it should be:

$hour = date('H') + $mhour;

The $mhour should be outside of the date function.

Chin Leung
  • 14,621
  • 3
  • 34
  • 58