I'm using PHP's time() to set two dates (default values for two input fields):
a start date, which should be the current time: date('m/d/Y H:i', time());
a end date, which should be the current time + 2 hours: date('m/d/Y H:i', time() + 60*60*2);
How can I adjust both dates, so the minutes divide with 5?
For example, if the current time is 12/12/2012 14:16, I want to adjust it to 14:20.
Or if the current time is 12/12/2012 04:59, I want to adjust it to 05:00.
$time = ceil(time() / 300) * 300;
echo date('m/d/Y H:i', $time);
echo date('m/d/Y H:i', $time + 60 * 60 * 2);
There might be other inbuilt function to achieve you problem , but here is the my solution to your problem
<?php
$minute = date('i');
$divident = ceil($minute/5);
$new_minute = $divident * 5;
$difference = $new_minute - $minute ;
date('m/d/Y H:i' ,time() + 60 * $difference ); // first date
date('m/d/Y H:i' ,time() + $difference * 60 + (2 * 60 * 60) ) // second date.
?>
I hope this helps :)
Prabeen
Related
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>';
I have looks like simple task, but I can't solve the problem. I would like to count two times in seconds by PHP ($time + $time1) but my result is +1hour (+1day). Why?
$hours="00";
$minutes="34";
$seconds="13";
$hours1="00";
$minutes1="35";
$seconds1="11";
$time=($hours*3600)+($minutes*60)+$seconds; // 2053 seconds
$time1=($hours1*3600)+($minutes1*60)+$seconds1; // 2111 seconds
$sum=$time+$time1; // 4164 seconds
$format=date('H:i:s', $time); // 01:34:13
$format1=date('H:i:s', $time1); // 01:35:11
$formatsum=date('H:i:s', $sum); // 02:09:24
$format has to be 00:34:13 not 01:34:13
If I add the days date(' d H:i:s', $time); result is 01 01:34:13
My task is 00:34:13 + 00:35:11 with result 01:09:24 not 02:09:24
Do I something wrong with time formating? Why it gives me +1day and +1hour to the result?
Change this:
$format=date('H:i:s', $time); // 01:34:13
$format1=date('H:i:s', $time1); // 01:35:11
$formatsum=date('H:i:s', $sum); // 02:09:24
To:
$format=gmdate('H:i:s', $time); // 00:34:13
$format1=gmdate('H:i:s', $time1); // 00:35:11
$formatsum=gmdate('H:i:s', $sum); // 01:09:24
gmdate — Format a GMT/UTC date/time
php doca gmdate
You should check your time zone. The time calculated is by your local time zone. As example, for
$time=($hours*3600)+($minutes*60)+$seconds; // 2053 seconds
$format=date('H:i:s', $time); // 02:34:13
For +2GMT
The result is 01:34:13, because your default timezone is set to UTC +1.
Use gmdate() function instead of date() if you want to convert a timestamp to a UTC date:
$hours = "00";
$minutes = "34";
$seconds = "13";
$hours1 = "00";
$minutes1 = "35";
$seconds1 = "11";
$time = ($hours * 3600) + ($minutes * 60) + $seconds; // 2053 seconds
$time1 = ($hours1 * 3600) + ($minutes1 * 60) + $seconds1; // 2111 seconds
$sum = $time + $time1; // 4164 seconds
$format = gmdate('H:i:s', $time); // 00:34:13
$format1 = gmdate('H:i:s', $time1); // 00:35:11
$formatsum = gmdate('H:i:s', $sum); // 01:09:24
Also, I'm not sure what you want to achieve with your code, but there are better tools for date/time manipulation than manual addition of seconds. Check out DateTime and DateInterval classes from PHP standard library.
I am unable to add random days hours minutes and seconds to the current date.
Here is my code-
$randnos=rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);
$newTime = date("d/m/Y H:m:s",strtotime(" +'.$randnos.' days" . " +'.$randhrs.' Hours". " +'.$randmin.' minutes". " +'.$randsec.' seconds"));
update_post_meta($post_id,'test',$newTime);
Your quote marks are all messed up.
$randnos=rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);
$newTime = date("d/m/Y H:m:s",strtotime("+$randnos days +$randhrs hours +$randmin minutes +$randsec seconds"));
update_post_meta($post_id,'test',$newTime);
One-liner
echo date('Y-m-d h:i:s', strtotime('+' .rand(30, 60 * 60 * 24 * 3).' seconds'));
Problem in the string used in strtotime().
$randnos=rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);
$str = ' +'.$randnos.' days +'.$randhrs.' Hours +'.$randmin.' minutes +'.$randsec.' seconds';
$newTime = date("d/m/Y H:m:s",strtotime($str));
Create the string first with random data, then insert in strtotime().
$randnos = rand(0,30);
$randhrs=rand(00,24);
$randmin=rand(00,60);
$randsec=rand(00,60);
$sec = $randsec + ($randmin * 60) + ($randhrs * 60 * 60 ) + ($randnos * 60 * 60 * 24);
$newTime = date("d/m/Y H:m:s",strtotime(date("r")) + $sec );
Hi I have a two variable in 24 hour time format and want to compute the number of hours worked. But I get negative and wrong value
I'm using PHP and here's my code
$endtime = date( 'g:i A', strtotime( $itInfo['endTime'] ) );
$startTime = date( 'g:i A', strtotime( $itInfo['startTime'] ) );
$timeDiff = (strtotime($endtime) - strtotime($startTime))/60/60;
$total = strtotime($endtime) - strtotime($startTime);
$hours = floor($total / 60 / 60);
$minutes = round(($total - ($hours * 60 * 60)) / 60);
echo "FROM ".$itInfo['startTime']." TO ".$itInfo['endTime']." (".$hours.'.'.$minutes."hours)";`
Here's the output FROM 22:00 TO 03:00 (-19.0hours) which is wrong the output should be 5 Hours.
Try this:
$timeDiff = strtotime($itInfo['endTime']) - strtotime($itInfo['startTime']);
echo substr('00'.($timeDiff / 3600 % 24),-2)
.':'. substr('00'.($timeDiff / 60 % 60),-2)
.':'. substr('00'.($timeDiff % 60),-2);
As others have stated, best to work with timestamps. But with your current code, you should be able to add this right before the echo:
if ($itInfo['startTime'] > $itInfo['endTime']) {
$hours = 24 - $hours;
}
This would be: 24 - 19 = 5.
Also, be sure to take add abs() in your $total variable as well:
$total = abs(strtotime($endtime) - strtotime($startTime));
I want to send a reminder email.I don't want to use cron on Linux/Unix/BSD box or Scheduled Tasks on Windows.
I'm trying to subtract 15 minutes from the current time.
here is my code so far (doesn't work):
$days = date("j",time());
$months = date("n",time());
$years = date("Y",time());
$hours = date("G",time());
$mins = (date("i",time()));
$secs = date("s",time());
$mins = $mins-15;
To subtract 15 minutes from the current time, you can use strtotime():
$newTime = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $newTime);
Change the date into a timestamp (in seconds) then minus 15 minutes (in seconds) and then convert back to a date:
$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);
You can use DateInterval
$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";
you can try this as well,
$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");
How about substracting the 15 minutes from time() before converting it?
$time = time() - (15 * 60);
And then use $time instead of time() in your code.
$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);
You can also use strtotime function to subtract days, hours and/or seconds from current time.
echo date('Y-m-d H:i:s', strtotime('-15 minutes'));
Following is the way you can add days / hours / minutes / sec to current time
$addInterval = date('Y-m-d H:i:s', strtotime("+$days days $hours hours $minute minute $sec second", strtotime(currentTime)));
You can also use DateInterval object
<?php
$date = new DateTime('Y-m-d H:i:s');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s');?>
Try using
$min = time() - 900; //900 seconds = 15 minutes
To subtract 15 minutes you can do:
date('Y-m-d H:i:s', (time() - 60 * 15));
You can replace 15 with the number of minutes you want.
In case you're looking to subtract seconds you can simply do:
date('Y-m-d H:i:s', (time() - 10));
In this way you'll subtract 10 seconds.
If you have only time value than below will be useful
// Your time
$time = '12:15:00';
// Returned value '12:00:00'
$newTime = date('H:i:s', strtotime($time) - (15*60));
I know this question is outdated but i just want to share how i did it in easy way
$current = new DateTime("10 minutes ago", new DateTimeZone('Asia/Manila') );
echo $current->format("Y-m-d H:i:s");
//To Get Current DateTime
$currentDate = date("Y-m-d H:i:s");
//To Get Current DateTime - 15Min
$oldDate = date("Y-m-d H:i:s", strtotime($currentDate) - (15 * 60));
echo $currentDate;
echo $oldDate;