I can't figure out why this is happening but when I added -10 mins to a time... it doesn't work.
This is a simplified version:
$time = '08:30';
$time_minus_10m = strtotime($time) + strtotime("-10 min");
echo '<b>'.$time.'</b> -10 mins is <b>'.date('H:i', $time_minus_10m).'</b>';
It outputs:
08:30 -10 mins is 01:05
but should output:
08:30 -10 mins is 08:20
Have you tried combining them?
$time_minus_10m = strtotime("$time -10 minutes");
In isolation, strtotime('-10 minutes') just gives you the time 10 minutes before the current time; adding that to any other time doesn't really make sense anymore :)
If you already have an existing timestamp to anchor against, you can pass that as the second parameter to be used instead of the current time.
Because you are subtracting 10 minutes from the current time since you don't provide the time in the strtotime() call. Try:
$time_minus_10m = strtotime("-10 min", strtotime($time));
$time = new DateTime('08:30');
$cloned_time = clone $time;
$time_minus_10m = $cloned_time->modify('-10 min');
echo '<b>'.$time->format('H:i').'</b> -10 mins is <b>'.$time_minus_10m->format('H:i').'</b>';
Related
How to add an odd day to a time in PHP?
Like add 1.5 day? Any idea?
When I try like 1.5 or 1,5 then it's adding 15 days.
Is there anyway to add odd day to time?
If there are fractions of days, they can be converted to seconds and then added.
$days = 1.5;
$seconds = (int)(86400 * $days);
$dt = new Datetime('2022-04-01');
$dt->modify($seconds.' Seconds'); //add
echo $dt->format('Y-m-d H:i');
//2022-04-02 12:00
Also works with $days = 1.25; This then returns 2022-04-02 06:00 as a result.
Or $days = 1.1 returns 2022-04-02 02:24.
Following is the one approach you can do first add an exact number of days(non-fractional part) and then add fractional value in hours.
For example, If you want to add 1.5 days then add 1 day and 12 hours.
echo date("Y-m-d H:i:s", strtotime('+1 Day +12 Hours'))
i want get only day, month, year, hour not include minute, second and subtraction it 5 hours. I search but result include minute and second
Ex: now 13/10/2015 18h and i want time now - 5 hours is 13/10/2015 13h
$dateTime = new DateTime();
$dateTime->modify('-5 hours');
echo $dateTime->format('d/m/Y h\h');
Read more about DateTime class
echo date('d/m/Y G', strtotime('-5 hour')) . 'h';
Have a look to strftime() :
<?
$time=time(); // This is the time in second from the epoch
$time -= 3600 * 5; // I subtract 5 hour
echo strftime("%d/%m/%Y %H", $time); // I print the calculated time
?>
I'm basically trying to add 24 hours to a date with php and display it but it keeps adding only 23 hours in stead of 24 hours.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo gmdate("Y-m-d H:i:s", $set_time);
?>
So the result that I'm getting out of this is:
2015-03-19 19:03:23
but it's this what should be coming out of this:
2015-03-19 20:03:23
I'm new at working with these time functions and I can't figure out why it keeps getting adding 23 hours. Obviously I can multiply it by 25 and get 24 hours but that doesn't make sense to me.
So my question is: what's the proper way to add 24 hours to a date?
I would do it like so:
date("Y-m-d H:i:s", strtotime("+1 day"));
strtotime() uses default time zone, gmdate() uses Greenwich Mean Time (GMT). Try using date() instead.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo date("Y-m-d H:i:s", $set_time);
?>
Is there a nice simple shorthand way of finding out how many seconds past midnight a certain datetime is? Not how many seconds it is away from now, the seconds past in that day
eg:
2015-04-10 00:00:00 should returns 0
2015-04-12 09:20:00 should returns 33600
2015-04-14 15:20:00 should returns 55200
Is there a nice short method of doing this?
Here is a simple code for you.
Any day code
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime(date("Y-m-d 00:00:00", strtotime($date)));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
Current day code
<?php
$midnight = strtotime("midnight");
$now = date('U');
$diff = $now - $midnight;
echo $diff;
?>
Maybe a more clean code would be to use strtotime("midnight", <EpochTime>);
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime("midnight", strtotime($date));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
A day is 24 hours is 60 minutes is 60 seconds is 1000 miliseconds, so timestamp % (24*60*60*1000) could work, maybe, at least if you are on the same time zone as the epoch php is using (or whatever you timestamp is coming from). It would work at least for nothing too demanding of accuracy. PHP uses your system's time, so many things will break the idea (change the clock, DST, leap seconds, etc)
I would personally use timestamp - startofday timestamp and calculate using the language's calendar instead.
In a validation there needs to be an interval of 180 minutes between each update, and as a callback I want to display how many minutes it's left until it's available for "updating" again.
I have this code:
time() - $user['last_update']
It gives me the unix time difference in seconds ($user['last_update'] is in unix time)
How can I get time() - $user['last_update'] to show that is it i.e. "120 minutes left"?
Thankful for input
Try this!:)
echo ((time() - $user['last_update'])/60)." minutes left";
Use DateTime class:
$now = new DateTime();
$time = new DateTime($user['last_update']);
$interval = $time->diff($now);
echo $interval->format("%i minutes remaining");
Demo!