How long time it is left? php + date - php

//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";
}
?>

Related

PhP date time addition

I have 2 variables.
The first -> ("01:10:00")(string);
The second -> ("2021-02-23 16:30:00")(string)
I want to add the two to be like "2021-02-23 17:40:00".
How can I accomplish this with PHP?
The answers was very much appreciated, thanks everyone.
You can do it by first exploding the time to add (first variable), then you finally add the exploded information to the date (second variable) using function strtotime:
<?php
$firstVar = "01:10:00";
list($hours, $minutes, $seconds) = explode(":", $firstVar);
$secondVar = "2021-02-23 16:30:00";
echo date('Y-m-d H:i:s',strtotime('+'. $hours .' hour +'. $minutes .' minutes +'. $seconds .' seconds',strtotime($secondVar)));
?>
You can do this:
<?php
$newtimestamp = strtotime('2021-02-23 16:30:00 + 70 minute');
// OR $newtimestamp = strtotime('2021-02-23 16:30:00 + 1 hour + 10 minute');
echo date('Y-m-d H:i:s', $newtimestamp); // Output: 2021-02-23 17:40:00
Anyway, if you need to convert HH:MM:SS to minutes, you can use the following function:
<?php
function minutes($time) {
$time = explode(':', $time);
return ($time[0]*60) + ($time[1]) + ($time[2]/60);
}
echo minutes('01:10:00'); // Output: 70
So finally you can do:
<?php
$datetime = '2021-02-23 16:30:00';
$add = minutes('01:10:00');
$newtimestamp = strtotime("$datetime + $add minute");
echo date('Y-m-d H:i:s', $newtimestamp);
function minutes($time) {
$time = explode(':', $time);
return ($time[0]*60) + ($time[1]) + ($time[2]/60);
}
A solution with DateTime. The time is converted into seconds and added using the modify method.
$timeArr = explode(':',"01:10:00");
$seconds = ($timeArr[0]*60 + $timeArr[1]) *60 + $timeArr[2];
$dateTime = date_create("2021-02-23 16:30:00")->modify($seconds." Seconds");
echo $dateTime->format("Y-m-d H:i:s");
This becomes even easier with the external class dt. This class has an addTime() method.
$dt = dt::create("2021-02-23 16:30:00")->addTime("01:10:00");
echo $dt->format("Y-m-d H:i:s"); //2021-02-23 17:40:00

Calculate date based on given time

I went through a project which I need to set a recall time for an event. the recall time is set to 1 day and some hours before the event happen. I calculate event date and recall time like this:
$now = time();
$days_to_event = 24 ; //(I set an input for it in form)
$event_time = $now + $days_to_event * 3600;
$difference = 24*3600 + time('h')*3600 + time('i')*60;
$recall_time = $event_time - $difference;
I was wondering if I could create a function which takes $recall_time and calculates the date and outputs it.
Any useful idea for simplifying the project is a great help for me.
Thank you my friends.
I think the answer you are looking for maybe is
echo date('Y-m-d H:i',$recall_time);
time() function return timestamp format, You need date() function to get hour and minute.
$now = time();
$days_to_event = 24; //(I set an input for it in form)
$event_time = $now + $days_to_event * 3600;
$difference = 24 * 3600 + date('h') * 3600 + date('i') * 60;
$recall_time = $event_time - $difference;
echo date('Y-m-d H:i', $now) . '<br>';
echo date('Y-m-d H:i', $event_time) . '<br>';
echo date('Y-m-d H:i', $recall_time) . '<br>';

PHP check if timestamp is greater than 24 hours from now

I have software that needs to determine if the cutoff datetime is greater than 24 hours from now. Here is the code I have to test that.
$date = strtotime("2013-07-13") + strtotime("05:30:00");
if($date > time() + 86400) {
echo 'yes';
} else {
echo 'no';
}
My current date and time is 2013-07-13 2am. As you can see its only 3 hours away.
At my math thats 10800 seconds away. The function I have is returning yes. To me this is saying the $date is greater than now plus 86400 seconds when in fact its only 10800 seconds away. Should this not be returning no?
$date = strtotime("2013-07-13") + strtotime("05:30:00");
should be
$date = strtotime("2013-07-13 05:30:00");
See difference in this CodePad
<?php
date_default_timezone_set('Asia/Kolkata');
$date = "2014-10-06";
$time = "17:37:00";
$timestamp = strtotime($date . ' ' . $time); //1373673600
// getting current date
$cDate = strtotime(date('Y-m-d H:i:s'));
// Getting the value of old date + 24 hours
$oldDate = $timestamp + 86400; // 86400 seconds in 24 hrs
if($oldDate > $cDate)
{
echo 'yes';
}
else
{
echo 'no'; //outputs no
}
?>
Store the values of date and time in separate variables and convert it into a Unix timestamp using strtotime() after concatenating the variables.
Code:
<?php
$date = "2013-07-13";
$time = "05:30:00";
$timestamp = strtotime($date." ".$time); //1373673600
if($timestamp > time() + 86400) {
echo 'yes';
} else {
echo 'no'; //outputs no
}
?>

Add 15 minutes to current time and snap to 15 minute intervals

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);

PHP add up two time variables

In my PHP application I want to calculate the sum of two time variables. I am looking for something like this example.
$time1 = 15:20:00;
$time2 = 00:30:00;
$time = $time1+$time2;
If the answer you expect is 15:50:00 and you want to use strtotime and date functions, you need to subtract the seconds $time1 and $time2 share when you transform them to unix timestamps:
$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1) + strtotime($time2) - strtotime('00:00:00');
$time = date('H:i:s', $time);
The best way to do this is most likely to use strtotime to convert them to timestamps and then do the adding together:
$o = strtotime($time1)+strtotime($time2);
If I remember right strtotime does support this format.
Otherwise you will need to filter it out yourself.
Following answer does not return correct value. It is summing integers but not returned correct time.
$o = strtotime($time1)+strtotime($time2);
I created a function to get calculated time as follows.
public function addTwoTimes($time1 = "00:00:00", $time2 = "00:00:00"){
$time2_arr = [];
$time1 = $time1;
$time2_arr = explode(":", $time2);
//Hour
if(isset($time2_arr[0]) && $time2_arr[0] != ""){
$time1 = $time1." +".$time2_arr[0]." hours";
$time1 = date("H:i:s", strtotime($time1));
}
//Minutes
if(isset($time2_arr[1]) && $time2_arr[1] != ""){
$time1 = $time1." +".$time2_arr[1]." minutes";
$time1 = date("H:i:s", strtotime($time1));
}
//Seconds
if(isset($time2_arr[2]) && $time2_arr[2] != ""){
$time1 = $time1." +".$time2_arr[2]." seconds";
$time1 = date("H:i:s", strtotime($time1));
}
return date("H:i:s", strtotime($time1));
}
You could use the PHP 5.3 DateInterval:
$timeInterval = DateInterval::createFromDateString( '15 hours + 20 minutes' );
$timeInterval2 = DateInterval::createFromDateString( '30 minutes' );
foreach( str_split( 'ymdhis' ) as $prop )
{
$timeInterval->$prop += $timeInterval2->$prop;
}
var_dump( $timeInterval->format( '%H:%i:%s' ) );
(How to add to DateInterval objects was explained here: How we can add two date intervals in PHP)
As far as I can tell, Sammaye's answer did't work out for me.
I needed to start the time I wanted to add with the start of the UNIX timestamp. This way, strtotime returns the seconds that need to be added to the first time.
$time1 = "15:20:00";
$time2 = "1970-01-01 00:30:00";
$time = strtotime($time1) + (strtotime($time2) + 3600);
echo $time . "<br />";
echo date("H:i:s", $time);
Be sure to consult the mystic docs http://us1.php.net/strtotime for additional things you can input into your functions :)
$today = time();
$tommorrow = strtotime("+1 days", $today);
$day_after_tomorrow = strtotime("+1 days", $tomorrow);
Code:
$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1)+strtotime($time2);
$sumtime = date("H:i:s",$time);

Categories