I have value 10:00:00 in my mor_start which is of type time in mysql database i want it to increment by 15 minutes.
$tim=$values['mor_start'];
$date = date('H:i:s', strtotime($tim));
echo $date;
the above code displays 10:00:00
but when i try to increment the time by the following code i get error : Notice: A non well formed numeric value encountered
$tim=$values['mor_start'];
$date = date('H:i:s', strtotime("+15 minutes",$tim));
echo $date;
As Mark Baker said (he should deserve the answer) one correct solution is replace the calculation by this :
strtotime("+15 minutes",strtotime($tim)));
Calling strtotime() multiple times is a little overkill. Code with DateTime extension is a faster example (and ofc more beautiful), like this demo:
echo date_create('10:00:00')->modify('+15 minute')->format('H:i:s');
Test this demo on your localhost and see the difference. My result on 1e6 loops is:
add15min_v1() has looped 1000000x times in 23.47sec.
add15min_v2() has looped 1000000x times in 6.81sec.
try this
$date = date('H:i:s', strtotime("+15 minutes", strtotime($tim)));
http://php.net/manual/en/function.strtotime.php
$tim=$values['mor_start'];
$date = date("H:i:s", strtotime("+15 minutes",strtotime($tim)));
echo $date;
This should work.
Related
How could I decrement the date in the text box by 1 day? The value of the dateschedule is 2016-04-02. The output should be 2016-04-01 because I want to decrement it by 1 day. I tried this code:
if(isset($_REQUEST["submit"]))
{
$dateschedule=security($_POST["dateschedule"]);
echo $new_time4 = date($dateschedule, strtotime('-1 day'));
}
the format of your date function is not going to work as you expect.. see php date manual
and as also posted on another SO question, you may use this:
$new_time4 = date('Y-m-d',(strtotime( '-1 day' , strtotime( $_POST["dateschedule"]))));
You can also achieve this by using DateTime.
$date = new DateTime('2016-04-02');
$date->modify('-1 day');
echo $date->format('Y-m-d');
//Output: 2016-04-01
https://3v4l.org/mD2GR
I find this alot more readable, you can easily see what you are doing and you can change your output easily with the format funciton.
I currently get duration in this format: 04:00 but I need in this format 00:04:00.
I am using this code but it's not working correctly.
$time = date("h:i:s", strtotime($duration));
echo $time;
try like this,
$duration = "00:"."04:00";
This should work for you:
(Your problem is in which format you read in your time, so DateTime::createFromFormat)
$date = DateTime::createFromFormat("i:s", "04:00");
echo $date->format('H:i:s');
Output:
00:04:00
The problem is the strtotime($duration); It doesn't know exactly what format that's in, and can't get a valid date from it. (This is why you're getting unexpected results from the date function.
If you want to just simply add 00: before $duration, you can do it like so:
$duration = '04:00';
$duration = '00:' . $duration;
You then would be able to pass this to the strtotime function, and would likely get better results.
I have a form that receives a time value:
$selectedTime = $_REQUEST['time'];
The time is in this format - 9:15:00 - which is 9:15am. I then need to add 15 minutes to this and store that in a separate variable but I'm stumped.
I'm trying to use strtotime without success, e.g.:
$endTime = strtotime("+15 minutes",strtotime($selectedTime)));
but that won't parse.
Your code doesn't work (parse) because you have an extra ) at the end that causes a Parse Error. Count, you have 2 ( and 3 ). It would work fine if you fix that, but strtotime() returns a timestamp, so to get a human readable time use date().
$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);
Get an editor that will syntax highlight and show unmatched parentheses, braces, etc.
To just do straight time without any TZ or DST and add 15 minutes (read zerkms comment):
$endTime = strtotime($selectedTime) + 900; //900 = 15 min X 60 sec
Still, the ) is the main issue here.
Though you can do this through PHP's time functions, let me introduce you to PHP's DateTime class, which along with it's related classes, really should be in any PHP developer's toolkit.
// note this will set to today's current date since you are not specifying it in your passed parameter. This probably doesn't matter if you are just going to add time to it.
$datetime = DateTime::createFromFormat('g:i:s', $selectedTime);
$datetime->modify('+15 minutes');
echo $datetime->format('g:i:s');
Note that if what you are looking to do is basically provide a 12 or 24 hours clock functionality to which you can add/subtract time and don't actually care about the date, so you want to eliminate possible problems around daylights saving times changes an such I would recommend one of the following formats:
!g:i:s 12-hour format without leading zeroes on hour
!G:i:s 12-hour format with leading zeroes
Note the ! item in format. This would set date component to first day in Linux epoch (1-1-1970)
strtotime returns the current timestamp and date is to format timestamp
$date=strtotime(date("h:i:sa"))+900;//15*60=900 seconds
$date=date("h:i:sa",$date);
This will add 15 mins to the current time
To expand on previous answers, a function to do this could work like this (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval):
(I've also written an alternate form of the below function here.)
// Return adjusted time.
function addMinutesToTime( $time, $plusMinutes ) {
$time = DateTime::createFromFormat( 'g:i:s', $time );
$time->add( new DateInterval( 'PT' . ( (integer) $plusMinutes ) . 'M' ) );
$newTime = $time->format( 'g:i:s' );
return $newTime;
}
$adjustedTime = addMinutesToTime( '9:15:00', 15 );
echo '<h1>Adjusted Time: ' . $adjustedTime . '</h1>' . PHP_EOL . PHP_EOL;
get After 20min time and date
function add_time($time,$plusMinutes){
$endTime = strtotime("+{$plusMinutes} minutes", strtotime($time));
return date('h:i:s', $endTime);
}
20 min ago Date and time
date_default_timezone_set("Asia/Kolkata");
echo add_time(date("Y-m-d h:i:sa"),20);
In one line
$date = date('h:i:s',strtotime("+10 minutes"));
You can use below code also.It quite simple.
$selectedTime = "9:15:00";
echo date('h:i:s',strtotime($selectedTime . ' +15 minutes'));
Current date and time
$current_date_time = date('Y-m-d H:i:s');
15 min ago Date and time
$newTime = date("Y-m-d H:i:s",strtotime("+15 minutes", strtotime($current_date)));
Quite easy
$timestring = '09:15:00';
echo date('h:i:s', strtotime($timestring) + (15 * 60));
Trying to convert standard time variable from form input to TIME format acceptable for MySQL INSERT. I might be going about it all wrong and could use some help. I've read through the MySQL TIME functions and PHP TIME functions but haven't found a solution yet.
Here's what I've tried as an example:
<?php
$time_input = '11:00 AM';
$strtotime = strtotime($time_input);
$mysql_time = date('H:m:s',$strtotime);
echo "<p>time_input: $time_input</p>";
echo "<p>strtotime: $strtotime</p>";
echo "<p>mysql_time: $mysql_time</p>";
?>
The result is changing the time to 11:08:00 (not sure where the 8 minutes is coming from):
time_input: 11:00 AM
strtotime: 1344438000
mysql_time: 11:08:00
Any help is much appreciated!
DateTime will do this for you:
$time_input = '11:00 AM';
$date = DateTime::createFromFormat( 'H:i A', $time_input);
$formatted = $date->format( 'H:i:s');
I typically avoid strtotime() when possible as it can be somewhat unpredictable.
You can see it work in this demo.
m is the format for the month with leading zeros (August, i.e. 08), you want i (minutes with leading zeros):
$mysql_time = date('H:i:s',$strtotime);
PHP date docs
I used :
<?php
echo date("H:i:s", $var_time_diff);
?>
to construct a time between two dates.. and in my head it was
$var_time_diff = 36000 = display 10:00:00 for 10 hours.
But in fact
<?php echo date("H:i:s", 0);?>
display 01:00:00 and not 00:00:00.
So we have
$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
All is ok for the moment $diff is 5 hours but if we display date like this:
echo date("H:i:s", $diff);
it will be "06:00:00".
So something wrong with my php config or it's a normal behavior for php function date?
The date() function uses your current time zone. If you want to ignore your configured time zone, use date_default_timezone_set() or use gmdate().
You're in some timezone other than UTC. Try:
<?php
date_default_timezone_set('UTC');
echo date("H:i:s",0) . "\n";
I'm not sure why, but date outputs the current hour in your example, make a timestamp from your seconds first and it works. I'll be following this question for a deeper explanation though.
$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
echo date("H:i:s", mktime(0,0,$diff));
edit: ah, so it adjusts to your current timezone, so does mktime, so the effect is negated.
use mktime, and min 1 for hour # $diff