1

Is there a way to convert an input time string (ex: 01:13) to a Zend date object, so that I store it later in a timestamp column in a Mysql database.

Examples:

If the current datetime is 2013-07-15 17:33:07 and the user inputs 18:05 the output should be 2013-07-15 18:05:00.

If the current datetime is 2013-07-15 17:33:07 and the user inputs 02:09 the output should be 2013-07-16 02:09:00. Notice that since the time entered was lower than the current time, so it was treated as tomorrows time.

I simply want to get the next point in time that satisfies the entered time. I'm open for solution using plain PHP or Zend_Date.

Songo
  • 5,618
  • 8
  • 58
  • 96

2 Answers2

1

I think you should compare the current time with the time entered by the user and create a DateTime object of either "today" or "tomorrow". DateTime accepts strtotime() relative time parameters.

Quick hack. Works as of today, 15.07.2013 23:58 local time:

$nextTime = new DateTime('today 18:10');
if ($nextTime < new DateTime('now')) { // DateTime comparison works since 5.2.2
    $nextTime = new DateTime('tomorrow 18:10');
}
echo $nextTime->format('d.m.Y H:i:s');
Sven
  • 69,403
  • 10
  • 107
  • 109
-1

here is working example for you just add your dynamic variable to check date with user inputs

You can use mktime function to manage your date.

$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d"),date("Y")));

echo "current time".$current_time = date('Y-m-d H:m:s');
echo "<br>User input is ".$input_date;

if(strtotime($current_time) > strtotime($input_date)){
    $input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d")+1,date("Y")));

    echo "in";
}else{
    // nothing to do
}
echo "<br> result->".$input_date;

i hope it will sure solve your issue

liyakat
  • 11,825
  • 2
  • 40
  • 46
  • 1
    You have a very subtle bug in your code. Calling `date()` multiple times for the same date will always use the current time of each of these function calls. You might end up calling the later `date()` just after the system time got to the next second, which might be the first second of a new day, a new month, or even a new year. – Sven Jul 16 '13 at 15:39
  • php execution is very fast then the other programming language so there want be any issue. – liyakat Jul 17 '13 at 03:04
  • You are denying the bug that is there. – Sven Jul 17 '13 at 07:29
  • One call to `date()` takes 0,000030041 seconds on my machine. The second call for the same day WILL occur later. If the first call was made on 23:59:59,99998 hours, the second call will be made after midnight, on a new day. The chances are small, but do exist. – Sven Jul 17 '13 at 07:37
  • your answer consist latest php version function, if some one has old version this will work, but why downvote ? – liyakat Jul 17 '13 at 08:02
  • Because your code has a bug. An evil bug. Nobody will notice. You do away with my arguments stating "PHP is fast enough". It is not. And by the way: My code works with any currently supported PHP versions. Which is 5.5, 5.4 and 5.3. I just updated to also support 5.2.2 - available now for more than 6 years. – Sven Jul 17 '13 at 08:11
  • ohh ok i am agree, :( – liyakat Jul 17 '13 at 08:52
  • will please send me link, how can we check php script execution time ? pls dont mind – liyakat Jul 17 '13 at 08:53
  • I don't think you get it. I'll stop wasting time now explaining. – Sven Jul 17 '13 at 09:10
  • i will appreciate you if you will help me to learn this things – liyakat Jul 17 '13 at 09:46