I have the time at which the person received the assignment (1555133436), I need to display the time after which he will be able to get them again (24 hours)
I tried to do this:
$date='1555133436';
$diff=date('H:i:s', 86400- (time() - $date));
echo $diff;
In response, I get some 15:24:41
Update
It seems from your comment that you want to find out how long a user must wait to be able to see the assignment again (24 hours after they first saw it). You can achieve that with this code, which creates a DateInterval object which represents that difference and then outputs the hours and minutes remaining:
date_default_timezone_set('UTC');
$date='1555133436';
$next_date = new DateTime("#$date");
// find out when they can next access the data
$next_date->modify('+1 day');
// now find out how long they must wait
$wait = $next_date->diff(new DateTime(), true);
echo $wait->format("You must wait %h hours and %i minutes to access the data again\n");
Output:
You must wait 21 hours and 38 minutes to access the data again
Demo on 3v4l.org
Original answer (looking for the time at which the data could be accessed again)
You can convert the timestamp into a DateTime object and then add 1 day to the value:
$date='1555133436';
$next_date = (new DateTime("#$date"))->modify('+1 day');
echo $next_date->format('Y-m-d H:i:s');
Output:
2019-04-14 05:30:36
If you want the result as a timestamp too, just use
echo $next_date->format('U');
Output:
1555219836
Note that you also get that result by simply adding 86400 to the original timestamp although you should note that this method won't work on days when daylight savings starts or ends.
echo $date + 86400;
Demo on 3v4l.org
Related
Given an arbitrary timestamp (e.g. 2019-02-26 10:30:00) I would like to find the next occurrence of an arbitrary time.
For example, the next occurrence of 12:00:00 will be 2019-02-26 12:00:00 but the next occurrence of 09:00:00 will be the next day at 2019-02-27 09:00:00. The results could be Carbon or Datetime objects. The test time will just be a string as shown.
Is there a way to calculate this in native PHP or PHP Carbon without conditionally boxing in time periods. An obvious way would be to see if the time being tested is past the check time for today, and if it is, taking the result as the check time plus 24 hours (the next day). That feels to me like too much chopping and joining of dates and times, so is there a way to calculate it by considering time to be a simple linear line?
All times will be in a single timezone, with DST. Note: the arbitrary datetimes and check times will stay clear of DST changeovers i.e. 01:00 to 02:00 so hopefully they will not be an issue to take into account.
Short answer is no for PHP (partial answer, I'm no specialist of Carbon but from quick look it's also no, but you can create a macro from following code).
However, with a ternary condition the one-liner is simple enough IMHO (replace the second DateTime($str) with DateTime() if you want to compare with current date and time, and change the >= by > if you want next day when time compared is exactly the same):
$str = '2019-02-26 10:30:00';
$date1 = ( ($a = (new DateTime($str))->setTime(12,00)) >= (new DateTime($str)) ) ? $a : $a->modify('+1 day');
$date2 = ( ($a = (new DateTime($str))->setTime(9,00)) >= (new DateTime($str)) ) ? $a : $a->modify('+1 day');
echo $date1->format('Y-m-d H:i:s'); //2019-02-26 12:00:00
echo $date2->format('Y-m-d H:i:s'); //2019-02-27 09:00:00
quick note: what you gave us is not a timestamp, but a formatted date.
Here is what I am using now through Carbon, which appears to give me the correct results:
$dateTime = Carbon::parse('2019-03-30 17:34:50', 'Europe/London');
$testTime = '16:00:00';
list ($hour, $minute, $second) = explode(':', $testTime);
$nextTimeOccurrence = $dateTime
->copy() // Carbon 1 only
->hour($hour)->minute($minute)->second($second);
if ($dateTime->gt($nextTimeOccurrence)) {
$nextTimeOccurrence = $nextTimeOccurrence->addDay();
}
// $nextTimeOccurrence is the next occurrence of $testTime after $dateTime
The splitting of the time seems clumsy, but might be the best way? The approach is:
Create a timestamp with the test time on the same day as the timestamp I'm checking. This will be the timestamp I am looking for.
If the timestamp I'm checking is after the timestamp created in the previous step, then add a day to it.
I've tested this around DST, and happily Carbon/Datetime keeps the same time when adding a day over a DST period, where a day there would be 25 hours or 23 hours, depending on which way it goes.
I still think there is a more "linear time" way to do this, but this seems simple and robust. Thanks go to #michael-stokoe here at the office for my lead on this.
How to read date_time_set as current time in php? it means when i run after 5 min it time should be 3:10
$date=date_create("2018-09-26");
date_time_set($date,3,5);
echo date_format($date,'y-m-d H:i:s')."\n";
//output 18-09-26 03:05:00.
Date objects don't really work that way. They're just for storing time values, they're not a clock. You're going to have to get the time at start via something like $start = time();. From there whenever you want to find out the elapsed time you could do a $elapsed = time() - $start; to get the number of seconds that have passed since the script started and then you're free to do the math from there or do something akin to $date->add($elapsed . ' seconds');
I am creating notifications system and here is a task: get the time, when a notification was sent. I mean the following: 1 minute ago, 13 hours ago and so on. I have already made up a script but it shows wrong time. For example instead of showing '5 minutes ago' it shows '9 hours ago'. Here is the alrogithm:
Get old timestamp from database. Old timestamp is the time, when a
notification was sent.
Get current users timestamp.
Get difference between them.
Echo result.
Here is the PHP code:
$fromdb = '1503737539'; //For this example think, that this variable is from database.
//This timestamp was created 5 minutes earlier, so in result it should show '5 minutes ago'.
$curr = new DateTime();
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
$interval = $curr->diff($got2);
echo $interval->format('%d')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes ".$interval->format('%s')." Seconds";
The output is:
0 days 9 hours ....
instead of
0 days 0 hours 5 minutes ....
How can I fix that? I guess that this is a problem with timezones. But how can I guess guests timezone though?
Update:
Change code:
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
And interesting fact: The more is actual difference, the less time it shows in output. For example: Old timestamp was created at 8:00 am, and current is 15:00 pm, it shows 0 days 1 hour in output.
I think
date('Y-m-d',$fromdb)
broke your code.
Try
$got2->setTimestamp($fromdb)
there is mistake in your code. you format date without hours, minutes and seconds
$got2 = new DateTime(date('Y-m-d',$fromdb));
try
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
Try
$date = new DateTime(); echo $date->format('U = Y-m-d H:i:s');
$date->setTimestamp(1171502725);
echo $date->format('U = Y-m-d H:i:s');
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.
i have to sent an email when a user register email contain a link that is become invalid after six hours
what i m doing when email is sent i update the db with field emailSentDate of type "datetime"
now i got the curent date and time and has made to the same formate as it is in db now i want to find that both these dates and time have differenc of 6 hours or not so that i can make link invalid but i donot know how to do this
my code is look like this i m using hardcoded value for db just for example
$current_date_time=date("Y-m-d h:i:s");
$current=explode(" ",$current_date_time);
$current_date=$current[0];
$current_time=$current[1];
$db_date_time="2010-07-30 13:11:50";
$db=explode(" ",$db_date_time);
$db_date=$db[0];
$db_time=$db[1];
i do not know how to proceed plz help
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
see http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
What you can do is convert both of your dates to Unix epoch times, that is, the equivalent number of seconds since midnight on the 31st of December 1969. From that you can easily deduce the amount of time elapsed between the two dates. To do this you can either use mktime() or strtotime()
All the best.
$hoursDiff = ( time() - strtotime("2010-07-30 13:11:50") )/(60 * 60);
I'd rather work with a timestamp: Save the value which is returned by "time()" as "savedTime" to your database (that's a timestamp in seconds). Subtract that number from "time()" when you check for your six hours.
if ((time() - savedTime) > 6 * 3600)
// more than 6h ago
or
"SELECT FROM table WHERE savedTime < " . (time() - 6 * 3600)
This might be the solution to your problem -> How to calculate the difference between two dates using PHP?