0

I have a given time and i need to create another time base on another given time. Let suppose i have given 4:00:00 AM, and another time is 2:00:00 , my result should be 6:00:00 AM, and 2:00:00 AM(based on condition). this is what i am using but its not giving correect result.

 if($data['turn_on_before_or_after'] == 'before'){
     $time = strtotime($data['sunset']) - strtotime($data['variation_turn_on']);
     $dataNew['final_turn_on'] = date('h:m:s',$time);
 }
 if($data['turn_on_before_or_after'] == 'after'){
     $time = strtotime($data['sunset']) + strtotime($data['variation_turn_on']);
     $dataNew['final_turn_on'] = date('h:m:s',$time);
 }
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
gaurav malik
  • 556
  • 1
  • 6
  • 17
  • is [this](https://stackoverflow.com/questions/20557059/php-adding-15-minutes-to-time-value) what you need? or do you need to add two separate times? if that is the case I would suggest converting them to seconds and add them up – hungrykoala Oct 02 '17 at 13:26
  • no @hungrykoala 2:00:00 in question could be vary from 00:30:00 to 12:00:00 on 30 minutes interval. – gaurav malik Oct 02 '17 at 13:29
  • What is it you are trying to accomplish here? – hungrykoala Oct 02 '17 at 13:30
  • i have a sunset time, sunrise time and a device that works automatically on sunrise and sunset. Now user has a option to customize sunrise and sunset time. suppose 2 hours ago or after. got it? – gaurav malik Oct 02 '17 at 13:33
  • I would suggest the link then you can customize it any way you want. it is not limited to +15 minutes only. – hungrykoala Oct 02 '17 at 13:35
  • @hungrykoala the link worked like charm. Thanks – gaurav malik Oct 04 '17 at 17:45

2 Answers2

0

Recommendation: use strtotime(). It will takes the date/time/datetime string and convert it to an integer; starting at the unix epoch. So 2AM would be 7200 and 4AM would be 14400; add those integers together and use date('H', $result) would convert the integer back into a time string. Boosh, win!

Opinion: many people will say unix timestamp is hard to use 'cause it is not human readable;I'd rather my logic be easy to read than the output. As the output only happens at the end of processing.

David J Eddy
  • 1,999
  • 1
  • 19
  • 37
0

I recreated your scenario, but instead of using strtotime I used the DateTime object.

Your main problem is that your first date ($data['sunset']) must be considered as a real date, but your second date ($data['variation_turn_on']) must be considered as an interval. Because of this, and after looking at the DateInterval object constructor, you notice that you can create an interval using sscanf from your initial string. After creating that interval, all you have to do is to use the methods from the DateTime class to simply add or substract intervals from a specific date.

Here is the code I wrote to obtain the results you expect (6:00:00 AM and 2:00:00 AM) :

<?php
    /* Initial parameters */
    $data['turn_on_before_or_after'] = "before";
    $data['sunset'] = "4:00:00 AM";
    $data['variation_turn_on'] = "2:00:00";

    /* Creating a list with your variation values */
    list($hours, $minutes, $seconds) = sscanf($data['variation_turn_on'], '%d:%d:%d');

    /* Creating the interval (here is the magic) */
    $intervale = new DateInterval(sprintf('PT%dH%dM%dS', $hours, $minutes, $seconds));

    /* Creating a DateTime object from your sunset time */
    $date = new DateTime($data['sunset']);

    /* Ternary for simplification, substract if before, add if everything else, you may use an if statement here */
    $data['turn_on_before_or_after'] == 'before' ? $date->sub($intervale) : $date->add($intervale);

    /* Printing the result */
    echo $date->format('h:i:s A');
Rafik Tighilt
  • 2,071
  • 1
  • 15
  • 27