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 );
Related
I'm trying to calculate the difference of two times. I'm using this method to return the difference in minutes.
$datetime1 = new DateTime('9.00am');
$datetime2 = new DateTime('10.15am');
$interval = $datetime1->diff($datetime2);
$timeDuration = $interval->format('%im');
However, the $timeDuration returns 15m instead of 75m.
Any help that could correct the code to let it return the exact correct difference in minutes ?
Because the difference between 9am and 10:15am is 1 hour ($interval->h) and 15 minutes ($interval->i), not 75 minutes literally.
If you wish to get the total number of minutes, you have to calculate it:
$differenceInMinutes = $interval->i + ($interval->h * 60);
If you wish to have more universal solution, I would go for the unix time:
$differenceInSeconds = abs($datetime1->format('U') - $datetime2->format('U'));
$differenceInMinutes = ceil($differenceInSeconds / 60);
The simple way to get different in minutes
<?php
$start = date_create('1990-01-01 09:00:00');
$end = date_create('1990-01-01 10:15:00');
$interval=date_diff($end, $start);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$in_minutes = $hours * 60 + $minutes;
echo 'Diff. in minutes is: '.$in_minutes;
?>
When you are trying to calculate the difference between two DateTime's like $datetime1 = new DateTime('9.00am'); and $datetime2 = new DateTime('10.15am'); you know that the difference is 75 minutes, but the resulto of diff() method is 1 hour for $interval->h and 15 minutes for $interval->i. So this is how you can calculate it:
$datetime1 = new DateTime('9.00am');
$datetime2 = new DateTime('10.15am');
$interval = $datetime1->diff($datetime2);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$difference_in_minutes = $hours * 60 + $minutes;
echo 'Difference in minutes is: ' . $difference_in_minutes . "\n";
There a working snipped in this link
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 3 fields in database startdate(datetime),global_time(time),enddate(datetime).Start date will be today datetime.
$start_date = date('Y-m-d h:i:s');
$global_time = '00:45:30' // comes from database global_time
Now i want to sum $start_date and $global_time and store into database field enddate.Can anyone suggest me to how to do this?
$start_date = new DateTime('2017-01-22 05:05:00');
$global_time = "00:45:30";
sscanf($global_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;//Converted in to seconds
$newtime="PT".$time_seconds."S";
$start_date->add(new DateInterval($newtime));
$enddate = $start_date->format('Y-m-d H:i:s');
echo $enddate;
PT0H45M30S for a interval of 0 hour, 45 minutes and 30 seconds.
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'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