i have this date and time i just want to get the millisecond until every 2:00 am.
example date/time: 10-18-2017 00:00:00 -- 2 hours before 2 am the millisecond before 2:00 am is 7200000.
what should i do or what method should i use. Thanks in advance
$datetimenow = date("m-d-Y H:i:s", strtotime('+0 hours'));
$TwoAMDay = time();
// add one day if next 2am is tomorrow
if(date('H', $TwoAMDay) >= 2) {
$TwoAMDay = $TwoAMDay + 86400;
}
$twoAMDate = date('Y-m-d 02:00:00', $TwoAMDay);
$twoAMTime = strtotime($twoAMDate);
$differenceMilliseconds = (1000 * $twoAMTime) - round(microtime(true) * 1000);
Related
I have the following code which outputs 20191027 as a result.
If I amend the 2nd line (i.e. set timezone to Auckland), it gives me the result 20191028. Why is this?
date_default_timezone_set("Europe/London");
#date_default_timezone_set("Pacific/Auckland");
$date_format = 'Ymd';
$day = "Sunday 4 week ago";
$start_of_the_week = strtotime($day);
$next_day = $start_of_the_week + (60 * 60 * 24 * 1);
$next_day = date($date_format, $next_day);
echo $next_day;
Check 2 outputs:
https://3v4l.org/A7ppT (20191027)
https://3v4l.org/Mfto3 (20191028)
In Europe/London timezone...
DST ended on Sun 27-Oct-2019 at 02:00:00 A.M. when local clocks were
set backward 1 hour
Keep in mind that that strtotime operates on unix timestamps where there is no concept of DST but the date function adjusts the unix timestamp to local timezone when formatting it. So:
$start_of_the_week = strtotime("Sunday 4 week ago"); // $start_of_the_week is some unix timestamp
echo date("Y-m-d H:i:s", $start_of_the_week); // 2019-10-27 00:00:00 Europe/London time
$next_day = $start_of_the_week + (60 * 60 * 24 * 1); // you're adding 24 hours to a unix timestamp
echo date("Y-m-d H:i:s", $next_day); // 2019-10-27 23:00:00 Europe/London time
And 2019-10-27 23:00:00 is still a Sunday. The solution is to add days instead of hours:
$next_day = strtotime("+1 day", $start_of_the_week); // 2019-10-28 00:00:00
As discussed in the comments, the issue is with Europe/London finishing daylight savings time on that day 4 weeks ago, so adding 24 hours to that time only took you forward by 23 hours. You can avoid issues like this by using DateTime objects and only working with days:
$date_format = 'Y-m-d H:i:s';
$day = "Sunday 4 week ago";
date_default_timezone_set("Europe/London");
$date = new DateTime($day);
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
date_default_timezone_set("Pacific/Auckland");
$date = new DateTime($day);
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
Output:
2019-10-28 00:00:00
2019-10-28 00:00:00
Demo on 3v4l.org
You can specify the timezone directly to the DateTime constructor too:
$date_format = 'Y-m-d H:i:s';
$day = "Sunday 4 week ago";
$date = new DateTime($day, new DateTimeZone("Europe/London"));
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
$date = new DateTime($day, new DateTimeZone("Pacific/Auckland"));
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
Demo on 3v4l.org
there is a difference between every time zone.
For Example
"India is 10 hours and 30 minutes ahead of Washington, DC, USA". If the echo the time of these time zones, it would end in giving different results.
In your case "Auckland, New Zealand is 13 hours ahead of London, UK", hence it is giving different O/P's
Hope this resolves your answer to the question :)
How would I structure the conditions to add two hours only to dates between 08:30 in the morning until 18:30 of the evening, excluding Saturday and Sunday?
In the case that a time near the border (e.g. 17:30 on Tuesday) is given, the left over time should be added to the beginning of the next "valid" time period.
For example: if the given date was in 17:30 on Tuesday, the two hour addition would result in 9:30 on Wednesday (17:30 + 1 hour = 18:30, 8:30 + the remainder 1 hour = 9:30). Or if the given date was in 17:00 on Friday, the result would be 9:00 on Monday (17:00 Friday + 1.5 hours = 18:30, 8:30 Monday + the remainder .5 hours = 9:00)
I know how to simply add two hours, as follows:
$idate1 = strtotime($_POST['date']);
$time1 = date('Y-m-d G:i', strtotime('+120 minutes', $idate1));
$_POST['due_date'] = $time1;
i have tried this this function and it works great except when i use a date like ( 2013-11-26 12:30 ) he gives me ( 2013-11-27 04:30:00 )
the problem is with 12:30
function addRollover($givenDate, $addtime) {
$starttime = 8.5*60; //Start time in minutes (decimal hours * 60)
$endtime = 18.5*60; //End time in minutes (decimal hours * 60)
$givenDate = strtotime($givenDate);
//Get just the day portion of the given time
$givenDay = strtotime('today', $givenDate);
//Calculate what the end of today's period is
$maxToday = strtotime("+$endtime minutes", $givenDay);
//Calculate the start of the next period
$nextPeriod = strtotime("tomorrow", $givenDay); //Set it to the next day
$nextPeriod = strtotime("+$starttime minutes", $nextPeriod); //And add the starting time
//If it's the weekend, bump it to Monday
if(date("D", $nextPeriod) == "Sat") {
$nextPeriod = strtotime("+2 days", $nextPeriod);
}
//Add the time period to the new day
$newDate = strtotime("+$addtime", $givenDate);
//print "$givenDate -> $newDate\n";
//print "$maxToday\n";
//Get the new hour as a decimal (adding minutes/60)
$hourfrac = date('H',$newDate) + date('i',$newDate)/60;
//print "$hourfrac\n";
//Check if we're outside the range needed
if($hourfrac < $starttime || $hourfrac > $endtime) {
//We're outside the range, find the remainder and add it on
$remainder = $newDate - $maxToday;
//print "$remainder\n";
$newDate = $nextPeriod + $remainder;
}
return $newDate;
}
I don't know if you still need this but here it is anyway. Requires PHP 5.3 or higher
<?php
function addRollover($givenDate, $addtime) {
$datetime = new DateTime($givenDate);
$datetime->modify($addtime);
if (in_array($datetime->format('l'), array('Sunday','Saturday')) ||
17 < $datetime->format('G') ||
(17 === $datetime->format('G') && 30 < $datetime->format('G'))
) {
$endofday = clone $datetime;
$endofday->setTime(17,30);
$interval = $datetime->diff($endofday);
$datetime->add(new DateInterval('P1D'));
if (in_array($datetime->format('l'), array('Saturday', 'Sunday'))) {
$datetime->modify('next Monday');
}
$datetime->setTime(8,30);
$datetime->add($interval);
}
return $datetime;
}
$future = addRollover('2014-01-03 15:15:00', '+4 hours');
echo $future->format('Y-m-d H:i:s');
See it in action
Here's an explanation of what's going on:
First we create a DateTime object representing our starting date/time
We then add the specified amount of time to it (see Supported Date and Time Formats)
We check to see if it is a weekend, after 6PM, or in the 5PM hour with more than 30 minutes passed (e.g. after 5:30PM)
If so we clone our datetime object and set it to 5:30PM
We then get the difference between the end time (5:30PM) and the modified time as a DateInterval object
We then progress to the next day
If the next day is a Saturday we progress to the next day
If the next day is a Sunday we progress to the next day
We then set our time to 8:30AM
We then add our difference between the end time (5:30PM) and the modified time to our datetime object
We return the object from the function
I have problem when i want calculate date. Simple Example:
I have 2013-09-01 is start date and I have 30day per month. My work i need alert tell to my user in 10 day before end month(it's mean on 2013-09-20i must alert message it's 10day more for end of this month). So every one have any idea for help to calculate it. becuese i like can't (+, -, *,/) on date. Now i am some data like
<?php
date_default_timezone_set('Asia/Phnom_Penh');
$current = time();
$start = 1380188957;
echo 'Start date: '. date('Y-m-d', $start) ."\n";
echo '<br/>';
$repeat = 30;
$enddate = time() + ($repeat * 24 * 60 * 60);
echo 'end date: '. date('Y-m-d', $enddate) ."\n";
Thanks in advent for helping.
Not every month has 31 days, you can get the number of days in any month by using the t option for the string format param in php's date() function.
// Current time as unix timestamp
$now = time();
// Number of days in current month
$days_this_month = date("t", time());
// Last day of the current month as a unix timestamp;
$end_of_month = strtotime(date("Y-m-t", time()));
// Ten days before the end of the month as a unix timestamp
$ten_days = strtotime('-10 days', $end_of_month);
Now we can do a check to see if it is 10 days before the end of the month:
if($now > $ten_days) {
// Do something
}
$start = 1380188957;
$enddate = time() + ($repeat * 24 * 60 * 60);
That's your own code. Using that we can easily compute 10 days before end date
$alert=$enddate-864000; // That's 10 days
$alertdate=date('Y-m-d', $alert);
I am computing for the time difference of night shift schedule using time only
lets say that I have this data:
$actual_in_time = 6:45 PM //date July 30, 2013
$actual_out_timeout = 7:00 AM //date July 31, 2013
I have to compute for the time difference where the time in should be converted to a whole time, therefore
$actual_in_time = //(some code to convert 6:45 PM to 7:00 PM)
$converted_in_time = $actual_in_time;
Now here is my code to that:
$actual_out_time += 86400;
$getInterval = $actual_out_time - $converted_in_time;
$hours = round($getInterval/60/60, 2, PHP_ROUND_HALF_DOWN);
$hours = floor($hours);
I am not getting the results I wanted. How do you compute for the time difference where the basis is just the time?
Using DateTime object
$start = new DateTime('2000-01-01 6:45 PM');
$end = new DateTime('2000-01-01 7:00 AM');
if ($end<$start)$end->add(new DateInterval('P1D'));
$diff = date_diff($start,$end);
echo $diff->format('%h hours %i minutes');
Add 1 day if your end time is less than start time.
You can use the second parameter in strtotime to get a relative time.
$start = strtotime($startTime);
$end = strtotime($endTime, $start);
echo ($end-$start)/3600;
I tried to look for this but I could not find good example of this what im trying to do.
I got datetime values in MySQL database that has to be rounded down when that value is on use.
Example, all these values:
2013-04-20 07:14:42
2013-04-20 07:19:51
2013-04-20 07:37:26
2013-04-20 07:46:28
2013-04-20 07:59:44
Should be rounded down to:
2013-04-20 07:00:00
And
2013-04-20 16:25:34
should be:
2013-04-20 16:00:00 etc...
PHP code that gets date value:
$d = strtotime($row["date"]);
So, how its possible to round down datetime value?
Try this,
$date = "2013-04-20 16:25:34";
echo date("Y-m-d H:00:00",strtotime($date));
CodePad Demo.
If you are using PHP's Carbon DateTime library (which you really should - https://github.com/briannesbitt/Carbon )
You can achieve it easily using
Carbon::now()->minute(0)->second(0);
Since you already have a Unix timestamp $d, most efficient way is to use only arithmetic instead of date functions - especially if you're going to loop through a result set.
$hourTimestamp = $d - ($d % 3600);
Modulo operator gives you the remainder which you subtract from the timestamp to get hour timestamp.
In that case a simple substr could do:
echo substr($date, 0, 13) . ":00:00";
Refering to #Dayson response, this is a better way to round datetime to last hour with Carbon
$dt = Carbon::create(2012, 1, 31, 15, 32, 45);
echo $dt->startOfHour(); // 2012-01-31 15:00:00
check the Modifier section in Carbon Doc
For those who follow (much later!): Carbon has an easy way to do this
$date = (new Carbon($row['date']))->minute(0)->second(0)->getTimestamp();
you can use
date and strtotime function function to achieve this, simply already change your minutes and second accordling
$date = '2013-04-20 07:14:42';
$newdate = date('Y-m-d H:00:00', strtotime($date));
echo $newdate;
this will output
2013-04-20 07:00:00
This writes the date to a string by outputting directly 00:00 as minutes and seconds instead of writing i:s:
$date = date("Y-m-d H:00:00", $d);
Or do you need it as unix timestamp? Then cut the minutes and the seconds off (always the last 5 bytes) and replace them by 00:00.
$d = strtotime(substr($row["date"], 0, -5)."00:00"));
strtotime() gives you a Unix timestamp which is the number of seconds since 1970-01-01 00:00:00.
What if just divided by 3600 seconds (seconds equivalent to 1 hour) and ignore the remainders (the minutes and seconds you do want)?
$d = strtotime($row["date"]);
$rounded_d = intval($d / 3600);
$formatted_rounded_d = date('Y-m-d H:i:s', $rounded_d)
I would use DateTime's setTime() method in a function like this.
<?php
/**
* #param \DateTime $dateTime
*
* #return \DateTime
*/
function roundDownToHour(\DateTime $dateTime)
{
$dt = $dateTime; // clone
return $dt->setTime(
$dateTime->format("H"),
0,
0
);
}
$testDate = date("Y-m-d H:i:s", mktime(9, 59, 59, 1, 30, 2019));
$roundedDownToHour = roundDownToHour(new \DateTime($testDate));
var_dump($testDate); //=> 2019-01-30 9:59:59
var_dump($roundedDownToHour->format("Y-m-d H:i:s")); //=> 2019-01-30 09:00:00
Which results to the following.
// the results
string(19) "2019-01-30 09:59:59"
string(19) "2019-01-30 09:00:00"
It's important you use a built-in PHP function for rounding times to take into account the date as well as the time. For example 2020-10-09 23:37:35 needs to become 2020-10-10 00:00:00 when rounding up to nearest hour.
Round time to nearest hour:
$time = '2020-10-09 23:37:35';
$time = date("Y-m-d H:i:s", round(strtotime($time) / 3600) * 3600); // 2020-10-10 00:00:00
$time = '2020-10-09 23:15:35';
$time = date("Y-m-d H:i:s", round(strtotime($time) / 3600) * 3600); // 2020-10-09 23:00:00
Round time to nearest 20 minute increment:
$time = '2020-10-09 23:15:35';
$time = date("Y-m-d H:i:s", ceil(strtotime($time) / (60*20))*(60*20)); // 2020-10-09 23:20:00
$time = '2020-10-09 23:41:35';
$time = date("Y-m-d H:i:s", ceil(strtotime($time) / (60*20))*(60*20)); // 2020-10-10 00:00:00
If you need to round down to nearest 20 minute increment, change ceil to floor e.g
$time = date("Y-m-d H:i:s", floor(strtotime($time) / (60*20))*(60*20)); // 2020-10-09 23:40:00
If you need to round time to another minute increment you can simply do:
$time = date("Y-m-d H:i:s", ceil(strtotime($time) / (60*15))*(60*15)); // 2020-10-09 23:45:00