Get the date from a time string

243 Views Asked by At

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.

2

There are 2 best solutions below

0
Sven On

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');
10
liyakat On

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