PHP Time going mental for one hour at midnight every day - php

I run this script which works sweet estimating how much time before something weather related happens. But a midnight it goes crazy and for the whole midnight hour, it returns crazy negative times like -1100 minutes and stuff, then when it gets to 0100 hrs it's back to normal and reports, like 20 mintues etc.
Script:
$timenow = date("H:i:s");
$eventtime= strtotime("$gettimefromtextfile"); //time the weather event will happen in the near future
$TimeEnd = strtotime($timenow);
$Difference = ($eventtime - $TimeEnd);
if ($Difference >= 0) {
$minutes = floor(($Difference / 60));
// print how many minutes until an event happens, discard it if event is in the past
I know the date function had issues with midnight up to PHP 5.3. But I am running PHP 5.3 so shouldn't be an issue. I don't need the date, it is only time I need, weather related stuff is reported only hours difference at most.
Any suggestions on an alternative function or coding that will stop this spasm at midnight?

What about using DateTime::diff? Don't reinvent the wheel!
<?php
date_default_timezone_set('Europe/Lisbon');
$next = new DateTime('18:00:01');
$now = new DateTime();
$diff = $next->diff($now);
echo $diff->format('%h hours, %i minutes');
?>
Reference: http://php.net/manual/en/datetime.diff.php

Related

PHP Show next 7 Days range

I saw this response to a thread and it works but doesn't create a range between the current day and seven days time. Which is what I need. Can anyone give me a hand please?
Update:
An easier way to word it. I want to Select data between two dates. For example current day and seven days time.
My current code:
if($current_day) {
$data['current_day']=date('Y-m-d', strtotime('+7 days'));
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You can use this code sample if you have php 5.3 or above,
Else try to calculate the difference of two dates in seconds using time() and strtotime(). Then translate those seconds into days/hours/minutes/seconds.

Facing issue while calculating the difference of time on different days

I have a cron which runs every minute calculates the difference between current time and an entry made. This cron will stop after business hours and run again when business hour starts.
I'm having an issue only when the entry is made on the evening (6pm) of tuesday and the difference is calculated with 1am of Wednesday.
How can I fix this?
Here is my code:
$ticket_created_timestamp = strtotime($ticket_created_time);
$current_time = date("H:i:s");
$current_timestamp = strtotime($current_time);
$time_difference_timestamp = $current_timestamp - $ticket_created_timestamp;
i don't see any particular problem here but my guess is - perhaps strtotime can't correctly interpret your ticket created time value
try something like that instead
$ticket_created_time = "2016-08-17 12:00:00";
$objDateCreatedTicketTime = DateTime::createFromFormat("Y-m-d H:i:s", $ticket_created_time);
$objDateTime = new DateTime();
$time_difference_timestamp = $objDateTime->getTimestamp() - $objDateCreatedTicketTime->getTimestamp();
The main advantage with this method is, that you've something standardized.
You can decide how your date input looks like without loosing the flexibility of handling your dates correctly.

PHP server side timeout check

Is there a simple straight forward way to find the remaining time between a start date - ex: 2013-01-10 12:34:55 and 5 minutes later?
What I mean is I have the start date and want to check that 5 or 60 minutes later gives a time difference of 0. Kind of a time out to be checked on server side.
You have 2 dates, correct ?
$date1 = strtotime('2013-01-10 12:34:33'); // converto to time
$date2 = strtotime('2013-01-10 12:45:33'); // or else it won't work
$diff = date('u', $date1) - date('u', $date2); // the difference in seconds between the two
// if you want $date2 to be now, just use date('u')
if ($diff > 3600) { // an hour later
echo 'The difference between $date1 and $date2 is more than an hour';
}
If you want to do something like "session time out" for the user. Use Javascript setintervel() and do ajax() call to logout user from the application.
Well, seems like I was going the harder way... found that it would be easier and more reliable to get it from an sql check than in php.
I've implemented this with success. Leave it here just in case it might be useful.
$query = "SELECT * FROM db_table
WHERE DATE_ADD(my_start_date, INTERVAL 5 MINUTE) > NOW()";
Where 5 is the interval that can be set in seconds (SECOND), minutes (MINUTE), hours (HOUR), days (DAY) etc.

Countdown with hours and minutes - compute the difference

I have two dates:
$today = '2012-12-01 10:40:00';
$check = '2012-12-03 12:00:00';
How can I show countdown for this dates?
Should show me:
Count: 49 hours and 20 minutes. I can check only hours or only minutes with function mktime, but how can i compare this?
try using DateTime::diff
<?php
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
?>
You can change these dates to a unix timestamp with strtotime.
Then you can calculate the difference between them in seconds.
60 seconds in a minute, 60 minutes in an hour.
Assuming you want the clock to keep ticking as the user stays on the page, you don't really want to do that using PHP (unless you want to dispatch AJAX calls to the server every second to update the clock, which would suck). Do it client-side, using javascript.
Here are 25 pretty scripts that do that using jQuery: http://www.tripwiremagazine.com/2012/11/jquery-countdown-scripts.html

php - Time remaining until specific time from current time of page load

I have to admit that having not even tried to code this myself this question may be a annoying to some but I'm very surprised that I wasn't able to find a good example on the web of what I'm trying to do. Perhaps I'm just not using the right key words in my searches.
I am trying to calculate the time remaining from now (the time the page loads) until a specific date and time (let's say Wednesday May 11, 2011 12:00PM for example) and display it. I had thought it would be quite easy to find a snippet to accomplish this but no luck so far. Does anyone have some example code they have used to accomplish this before? I don't expect anyone to write this for me from scratch but if I can at least get pointed in the right direction it would be extremely helpful.
I'd use the DateInterval and DateTime functions:
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You'll need a version of PHP that's at least 5.3 to do it this way - otherwise, do what helloandre recommends.
I think it will usefull
$startdate="2008-06-22 20:38:25";
$enddate="2008-06-29 21:38:49";
$diff=strtotime($enddate)-strtotime($startdate);
echo "diff in seconds: $diff<br/>\n<br/>\n";
// immediately convert to days
$temp=$diff/86400; // 60 sec/min*60 min/hr*24 hr/day=86400 sec/day
// days
$days=floor($temp); echo "days: $days<br/>\n"; $temp=24*($temp-$days);
// hours
$hours=floor($temp); echo "hours: $hours<br/>\n"; $temp=60*($temp-$hours);
// minutes
$minutes=floor($temp); echo "minutes: $minutes<br/>\n"; $temp=60*($temp-$minutes);
// seconds
$seconds=floor($temp); echo "seconds: $seconds<br/>\n<br/>\n";
echo "Result: {$days}d {$hours}h {$minutes}m {$seconds}s<br/>\n";
echo "Expected: 7d 0h 0m 0s<br/>\n";
echo "time isss".time();
echo $date = date('Y-m-d H:i:s')";
?>
first you'll need to calculate the difference in seconds using time() and strtotime(). Then you can translate those seconds into days/hours/minutes/seconds.

Categories