How would i count down to a specific hour every day, say 12PM EST time... So that it would countdown as 1 Hour 56 Minutes until 12PM / Lunch? I'm looking for something short and simple, no need for CSS or JS as it will be text and only update when the page is refreshed.
I'm not sure if you have heard, but there's this trillion dollar site called google.com. You can search terms that will give you the best results on the web! Try it sometime, hella useful.
Check it out, here's the first result from it!
http://www.tripwiremagazine.com/2011/04/9-cool-jquery-countdown-scripts.html
Here's what i was looking for in case it will help someone else.
function countdown($year, $month, $day, $hour, $minute)
{
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
$current = time();
$difference = $the_countdown_date - $current;
if ($difference < 0) $difference = 0;
$days = floor($difference/60/60/24);
$hours = floor(($difference - $days*60*60*24)/60/60);
$minutes = floor(($difference - $days*60*60*24 - $hours*60*60)/60);
echo "Checkback in ".$hours." hours and ".$minutes." minutes";
}
countdown(2016,1,1,12,0);
Related
I'll try to explain my problem. I am beginner with PHP and need help manipulating Date/Time.
So here is my situation.
I get Date/Time values from the database in this format: 07/02/2017 11:00 pm
First I need to calculate a difference between two dates and output duration.
Then add up duration and output total time.
The code is very dirty as I am just researching now. I also came to a problem that DateTime does no carry over points. As far as understand I need to convert days to hours and add them up.
Can anybody more experienced make sense of this?
$total_time = new DateTime('00:00');
$start_date = new DateTime('2017-05-01 12:20 PM');
$end_date = new DateTime('2017-05-01 12:30 PM');
$interval = $start_date->diff($end_date);
$total_days = $interval->days;
$hours = $interval->h;
if ($total_days !== FALSE) {
$hours += 24 * $total_days;
}
$minutes = $interval->i;
$total_time ->add($interval);
echo $hours .'hours ' . $minutes . 'minutes';
echo $total_time->format('h:i'); ?>
You can add a DateInterval to the DateTime.
You can check on http://php.net/manual/en/datetime.add.php
Looking at your code:
$total_hours = 0;
$total_minutes = 0;
//must be initialized outside of the while loop
$start_date = new DateTime('2017-05-01 12:20 PM');
$end_date = new DateTime('2017-05-01 12:30 PM');
$interval = $start_date->diff($end_date);
$hours = $interval->h + 24*$interval->d; /*there is no need to check
if you have total days, since 0 days would still work as expected */
$minutes = $interval->i;
echo 'Duration: '.$hours.' hours '.$minutes.' minutes';
$total_hours += $hours;
if(($total_minutes += $minutes) >= 60) {
$total_hours += 1;
$total_minutes -= 60;
}
//after the end of the while loop
echo 'Total time:'.$total_hours.':'.$total_minutes;
Now I don't understand what is the expected output of total time, if you can elaborate on what is not working it would be easier to help
I am using following code
list($date, $time) = explode(' ', $row['created_at']);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
$timemodified = mktime($hour, $minute, $second, $month, $day, $year);
$threshold = time() - 6;
echo $threshold.'</br>';
echo $timemodified.'</br>';
echo $timemodified - $threshold;
It outputs
1428631618
1428643990
12372
The modified time is just two minutes ago. Why is the difference so big I am just subtracting six seconds. Am I missing sommething?
It's because the $threshold time is not really 6 seconds. You can use strtotime() function to subtract 6 seconds from your time
$newTime = strtotime('-6 seconds', $timemodified);
echo date('Y-m-d H:i:s', $newTime);
hope this helps. For my example see this: http://codepad.org/cRp858RG
I need to get the age of a person. In case of an infant <= 7 days the output should be done in days. If the person is > 2 months and < 1 year the output should be months.
Here I got the problem, that some months are 31 other 30 or 28 days, so my solution isn't exact. Same problem for the else-case: Years with 366 days are ignored by my attempt, so the age isn't calculated correctly.
$timestamp = time();
$birthday_timestamp = mktime(0, 0, 0, $month, $day, $year);
$difference = $timestamp - $birthday_timestamp;
if ($difference < 1209600) return $output = ($difference / 86400)." days";
elseif ($difference < 5184000) return $output = ($difference / 86400 * 7). " weeks";
elseif ($difference < 31536000) return $output = ($difference / 86400 * 30). " months";
else return $output = ($difference / 86400 * 365). " years";
Don't try to calculate dates and differences between dates yourself. PHP has some very nice classes to do that for you.
$now = new DateTime();
$birthDay = new DateTime('1985-05-24');
$diff = $birthDay->diff($now);
var_dump($diff);
This is safe and takes into account leap years and other strange things that will occur when calculating with dates.
$diff will be a DateInterval and contains properties like $y, $m and $d.
function crimemaketime($until){
$now = time();
$difference = $until - $now;
$days = floor($difference/86400);
$difference = $difference - ($days*86400);
$hours = floor($difference/3600);
$difference = $difference - ($hours*3600);
$minutes = floor($difference/60);
$difference = $difference - ($minutes*60);
$seconds = $difference;
$output = "$minutes Minutes and $seconds Seconds";
return $output;
}
Hi, im looking to set and interval so that no refreshing is need for my timers.
This i have above works fine for my output, but im unable to get it to work with setinterval.
ive searched the internet for a good few hours and nothing has seemed to work or maybe im doing something wrong.
Any help is appreciated, many thanks.
Scenario: An record was entered into the database.
I am trying to figure out the following equations:
How to get the number of hours since the record was added.
How to get how many hours are left until midnight since the record
was added.
Given these times:
Date / Time: 2012-08-22 20:11:20
Time Stamp: 1345684280
Midnight Tonight: 2012-08-23 00:00:00
Midnight Time Stamp: 1345698000
I feel like I'm on the right track. Just need some proper math to do the calculations? I am horrible at math. Any help or guidance would be appreciated. I'm not looking for someone to COMPLETE THIS FOR ME. Just looking for advice on what I'm doing wrong, or how I could do it better. Maybe explain the math formulas necessary to achieve my goal.
Here is what I have so far:
class tools{
public function __construct(){
}
public function check_time($time, $request){
$time = strtotime($time);
if($request == 'since'){
$theTime = time() - $time;
$prefix = 'Since:';
} elseif($request == 'until'){
$midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
$theTime = $midnight - $time;
$prefix = 'Until:';
}
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach($tokens as $unit => $text){
if($theTime < $unit) continue;
$duration = floor($theTime / $unit);
return $prefix.' '.$duration.' '.$text.(($duration>1)?'s':'');
}
}
}// EoF tools class
$tools = new tools();
print_r($tools->check_time('2012-08-22 20:11:20', 'since'));
print_r($tools->check_time('2012-08-22 20:11:20', 'until'));
The solution here is very simple. There is a minor error that's causing all of your issues.
In your code you have this to calculate midnight.
$midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
This is incorrect for the simple fact that it's using TODAY's midnight (Which would have been 00:00 today, however many hours ago from now. You want midnight TOMORROW, since it's considered 00:00 on 24 hour time and is tomorrow. The correct way to do it is just like this:
$midnight = strtotime("tomorrow 00:00:00");
Just keep in mind that strtotime() bases everything off of GMT, so make sure you set a default timezone in the file/application.
I hope my answer is clear and explains why the code you posted is wrong and how to fix it.
Maybe something like this? I have to admit to not completely understanding what your desired output is:
$d1 = new DateTime('2012-08-22 20:11:20');
$d2 = new DateTime('2012-08-23 00:00:00');
$interval = $d1->diff($d2);
echo $interval->format('%h hours %i minutes and %s seconds');
Nice and easy:
$timeLeft = 86400 - (time() - strtotime("today"));
echo date("H:i:s", $timeLeft);
86400 is the initial time of a day.
time() is the current time.
strtotime("today") is the starttime of this day.
date("H:i:s", $timeLeft) is for the formatting in hours, minutes and seconds.
Even shorter way:
date("H:i:s", strtotime("tomorrow") - time())
midnight is not more than the next day without specifying time the best way to do it must be :
<?php
$datetime1 = new DateTime(date('Y-m-d H:i:s'));//current datetime object
$datetime2 = new DateTime(date('Y-m-').date(d));//next day at midnight
$interval = $datetime1->diff($datetime2);//diference
echo $interval->format('H');printing only hours (same as date format)
?>
if you want to know more : php date_diff
You can try this:
$now = strtotime('2012-08-22 20:11:20');
$midnight = strtotime('2012-08-23 00:00:00');
$difference = $midnight - $now;
echo date("H:i:s", $difference);