I need to add 15 minutes to the current time.
For eg : now is 20:48 , need to add 15 minutes, so now it will be 21:03, but i need to set 21:15 ,that is , it should be in multiples of15,30,45,00.
Help help/guidance would be of good help.
<?php
$current_date_time = date('d/m/Y H:i:s');
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date;exit;
Here's a simple example
//what time is it?
$t=time();
//how long is our interval?
$interval=15*60;
//we can calculate when the last interval started by subtracting $t % $interval
$last = $t - $t % $interval;
//so now we know when the next interval will start...
$next = $last + $interval;
echo "Next interval at ".strftime('%H:%M:%S', $next)."\n";
You look like you might want to add 2*$interval to the last interval, but you should be able to adapt this to suit you.
Just to correct my post:
$time = time();
$last_time = ($time - ($time % (15 * 60)));
$in15mins = $last_time+ (15 * 60);
echo 'Now: '. date('Y-m-d H:i') ."\n";
echo 'in 15 mins: '. date('Y-m-d H:i', $in15mins) ."\n";
I think it is quite self explaining. Optimize it, use it.
$current_date_time = date('d/m/Y H:i:s');
echo $current_date_time."<br>";
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date."<br>";
$minutes = date("i",strtotime($current_date));
$min = '';
if($minutes > 0 && $minutes <15){
$min = 15 - $minutes;
} else if($minutes > 15 && $minutes <30){
$min = 30 - $minutes;
} else if($minutes > 30 && $minutes <45){
$min = 45 - $minutes;
} else {
$min = 59 - $minutes;
$min++;
}
$newdate = date("d/m/Y H:i", strtotime($current_date."+".$min." minutes"));
echo $newdate;
Use the above code. this is working for me.
$currentTimeStamp=strtotime('now');
$nextInterval=date("Y-m-d H:i", strtotime("+15 minutes", $currentTimeStamp));
dump($nextInterval);
Related
I have a PHP and MySQL code that should calculate the hours minutes and days of difference between two date and hours. It works well, just adding 20 hours and 20 minutes more than normal. And I remove the DATE part and put the date and time manually, it works fine.
I don't understand what happens.
$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $hora");
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));
$end = strtotime("$currentDate");
$totaltime = ($end - $start) ;
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == Start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}
Edit
start 2019-12-29 21:27:50 . end 2019-12-31 0:51:50 = 47:51:16
As you can see it calculates badly.
A simple example like this would do the job :
$mydatetime = new DateTime();
$datefromdb = new DateTime('2018-03-05 10:10:00');
$interval = $mydatetime->diff($datefromdb);
$date_count = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $date_count;
This is your code it should work
$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $data");
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));
$end = strtotime("$currentDate");
$totaltime = ($end - $start) ;
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == $start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}
problem was in the string.
$data= $row["hora"];
and I use this
$start = strtotime("$fecha $hora");
And don't take the hours and calculate only for days.
Thank you
I'll try to explain my problem. I am beginner with PHP and need help manipulating Date/Time.
So here is my situation.
I get Date/Time values from the database in this format: 07/02/2017 11:00 pm
First I need to calculate a difference between two dates and output duration.
Then add up duration and output total time.
The code is very dirty as I am just researching now. I also came to a problem that DateTime does no carry over points. As far as understand I need to convert days to hours and add them up.
Can anybody more experienced make sense of this?
$total_time = new DateTime('00:00');
$start_date = new DateTime('2017-05-01 12:20 PM');
$end_date = new DateTime('2017-05-01 12:30 PM');
$interval = $start_date->diff($end_date);
$total_days = $interval->days;
$hours = $interval->h;
if ($total_days !== FALSE) {
$hours += 24 * $total_days;
}
$minutes = $interval->i;
$total_time ->add($interval);
echo $hours .'hours ' . $minutes . 'minutes';
echo $total_time->format('h:i'); ?>
You can add a DateInterval to the DateTime.
You can check on http://php.net/manual/en/datetime.add.php
Looking at your code:
$total_hours = 0;
$total_minutes = 0;
//must be initialized outside of the while loop
$start_date = new DateTime('2017-05-01 12:20 PM');
$end_date = new DateTime('2017-05-01 12:30 PM');
$interval = $start_date->diff($end_date);
$hours = $interval->h + 24*$interval->d; /*there is no need to check
if you have total days, since 0 days would still work as expected */
$minutes = $interval->i;
echo 'Duration: '.$hours.' hours '.$minutes.' minutes';
$total_hours += $hours;
if(($total_minutes += $minutes) >= 60) {
$total_hours += 1;
$total_minutes -= 60;
}
//after the end of the while loop
echo 'Total time:'.$total_hours.':'.$total_minutes;
Now I don't understand what is the expected output of total time, if you can elaborate on what is not working it would be easier to help
How can I get the last/previous half hour mark using PHP date/time.
So the time 15:31 all the way through to 16:29 should give 15:30 since that's the last half hour mark.
And I want to be able to dynamically change the minutes.
So if I set the minutes to 15, it'll get the last/previous :15 minute mark
So 15:16 through to 16:14 will return 15:15
This is what I have but I just can't get it to work...
function newtime($time, $offset) {
$prev = $time - $offset;
$newtime = ($offset > 900) ? ($prev - 1800) : ($prev);
return $newtime;
}
$hours = date("H", time());
$minutes = date("m", time());
echo 'Current date: ' . date("Y-m-d H:i:s", time()) . '<br/>';
$time = strtotime("2013-01-23 15:35:00");
echo 'time: ' . date("Y-m-d H:i:s", $time) . '<br/>';
$offset = ($time % 1800);
echo 'prev: ' . $offset . '<br/>';
$newtime = newtime($time, $offset);
echo date("Y-m-d H:i:s", $newtime) . '<br/><br/>';
It's really tough, I just can't wrap my head around it.
Any help is greatly appreciated!
Try something like this:
<?
function newtime($time,$minute=30){
$time=strtotime($time);
$m=date("i",$time)*1;
$h=date("H",$time)*1;
if($m<$minute){
$h=$h-1;
}
return date("H:i",strtotime($h.":".$minute));
}
print newtime("15:31");
//OUTPUT: 15:30
print newtime("16:29");
//OUTPUT: 15:30
print newtime("15:16",15);
//OUTPUT: 15:15
print newtime("16:14",15);
//OUTPUT: 15:15
?>
function newtime ($time, $offset)
{
// First, calculate the offset in seconds:
$offset_sec = $offset * 60;
// Next, fetch unix timestamp from $time
$unix_time = strtotime($time);
// Then calculate the modulo
$modulo = $unix_time % $offset_sec;
// Calculate latest timestamp
$last_time = $unix_time - $modulo;
// Display latest timestamp
return date('H:i',$last_time);
}
//Example data
$current_time = 1318075950;
$unbanned_time = $current_time + strtotime('+1 minute');
if ($unbanned_time > $current_time) {
$th1is = date('Y-m-d H:i:s', $unbanned_time) - date('Y-m-d H:i:s', $current_time);
echo date('Y-m-d H:i:s', $th1is);
I am trying to output how long time it is until the user is unbanned... year months, days, hours, minutes and seconds... But this is giving me some weird results..
You should check manual on how to work with date/time functions.
First of all, instead of
$current_time + strtotime('+1 minute')
use
strtotime('+1 minute', $current_time);
(see manual on strtotime).
Secondly, date function returns a string. Subtracting two strings is not really useful in most cases.
if ($unbanned_time > $current_time) {
$th1is = $unbanned_time - $current_time;
echo $th1is/3600 . ' hours';
}
This will output the remaining time in hours but there are many functions available that will produce better formatting (or you can code one for yourself).
I would recommend to use DateTime
$DateTime = new DateTime();
$unbanned_DateTime = new DateTime();
$unbanned_DateTime = $unbanned_DateTime->modify('+1 minute');
if ( $unbanned_DateTime > $DateTime ) {
$interval = $DateTime->diff($unbanned_DateTime);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%s');
}
Instead of using every single value as variable you can use ->format() for one output. As you like.
Remember DateTime->format() needs a timezone setting up in your php.ini or with
date_default_timezone_set('....');
date() returns a string, substracting two strings makes no sense here. You can use basic maths to calculate the remaining time:
<?php
$current_time = time();
$unbanned_time = /* whatever */;
$seconds_diff = $unbanned_time - $current_time();
echo "You're unbanned at " . date("Y-m-d H:i:s", $unbanned_time) . " which is over ";
if ($seconds_diff <= 120) {
echo "$seconds_diff seconds";
} else if ($seconds_diff <= 7200) {
echo floor($seconds_diff / 60) . " minutes";
} else if ($seconds_diff <= 7200 * 24) {
echo floor($seconds_diff / 3600) . " hours";
} else {
echo floor($seconds_diff / 3600 / 24) . " days";
}
?>
Hay, i have a database holding events. There are 2 fields 'start' and 'end', these contain timestamps. When an admin enters these dates, they only have the ability to set the day,month,year. So we are only dealing with stamps containing days,months,years, not hours,minutes,seconds (hours,minutes and seconds are set to 0,0,0).
I have an event with the start time as 1262304000 and the end time as 1262908800. These convert to Jan 1 2010 and Jan 8 2010. How would i get all the days between these timestamps? I want to be able to return Jan 2 2010 (1262390400), Jan 3 2010 (1262476800) .. all the way to the end stamp. These events could cross over into different months, say May 28 to June 14.
Any ideas how to do this?
You just have to calculate the number of seconds between the two dates, then divide to get days :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
Then, you can use a for loop to retrieve the dates :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
for ($i = 1; $i < $numDays; $i++) {
echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '<br />';
}
Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).
I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.
//1 day = 86400 seconds
I would build an array of the days to use later.
EDIT (example)
$difference = 86400;
$days = array();
while ( $start_time < $end_time )
{
$days[] = date('M j Y', $end_time);
$end_time -= $difference;
}
This should cover any time frame even if its over a bunch of months.
Try this:
while($date_start <= $date_end) {
echo date('M d Y', $date_start) . '<br>';
$date_start = $date_start + 86400;
}
Hope this helps !
$d1=mktime(22,0,0,1,1,2007);
$d2=mktime(0,0,0,1,2,2007);
echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds difference = " .($d2-$d1). "<br>";
echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";
http://www.plus2net.com/php_tutorial/date-diff.php
http://www.phpf1.com/tutorial/php-date-difference.html
$daysInBetween = range($startTs, $endTs, 86400);
$secondDay = date('M d Y', $daysInBetween[1]);
/*
$thirdDay = date('M d Y', $daysInBetween[2]);
...
*/
Note that the range() function is inclusive.
**This is a very simple code for find days hours minutes and seconds in php**
$dbDate = strtotime("".$yourbdDate.""); // Database date
$endDate = time(); // current time
$diff = $endDate - $dbDate; /// diffrence
$days = floor($diff/86400); /// number of days
$hours = floor(($diff-$days*86400)/(60 * 60)); //// number of hours
$min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute
$second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes
if($days > 0) echo $days." Days ago";
elseif($hours > 0) echo $hours." Hours ago";
elseif($min > 0) echo $min." Minute ago";
else echo "Just second ago";
Something like this?
$day = $start;
while ($day < $end) {
$day += 86400;
echo $day.' '.date('Y-m-d', $day).PHP_EOL;
}
By the way, 1262304000 is Dec 31, not Jan 1.
get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result