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');
Related
i am new to stackOverflow, i was wondering how could i get the missing time date to a microtime(true) starting from another microtime(true), and get it formatted like that (H:i:s).
This is my code:
$rewardCountdown = microtime(true) - $this->dailyRewardTime; // daily reward is another microtime(true)
$rewardAvailable = $rewardCountdown >= 60 * 60 * 24; // check if 24h are gone so we can get reward
Basically i want to get the $rewardCountdown in this format (H:i:s)
I tried and somehow got something like that but i was getting the time increasing instead of decreasing
The current microtime minus a previous microtime would be a positive value, of the number of seconds passed between the two times.
If you want to convert seconds into Hours, Minutes and Seconds, you can simply use gmdate.
$rewardCountdown = microtime(true) - $this->dailyRewardTime;
$date = gmdate('H:i:s', $rewardCountdown);
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
I am making a game mode in which I am trying to get the time a player has arrived at their destination after starting the mode and to do this I am using the insert of a date when it starts the mode it inserts a date and after reaching the your destination it registers another date and with both dates it calculates the time it took to get to the destination, with this I'm using date H:i:s (hours, minutes, seconds) but I need to take the time out and leave milliseconds after seconds example: i:s:u (minutes, seconds, milliseconds) but I'm not able to do this, I've tried it in several ways, basically everything works as follows:
1. I add in the player array a current date with hour, minutes, seconds;
$this->game[$player->getName()] = ["start" => strtotime('now')];
2. After the Player arrives at his destination he calculates the time of his trajectory creating another current date with already registered and using date and mktime to do the join and give a visual of time to the player;
$time = date('H:i:s', mktime(0, 0, str_replace("-", "", $this->game[$player->getName()]["start"] - strtotime('now'))));
3. Send the pretty message to the player about the time of his trajectory then time will be something like this: 01:45:23 (minute:seconds:milliseconds).
$player->sendMessage("You beat your time record by ".$time);
This is my method of doing, if you have another better method with the milli seconds added I accept the suggestion! Maybe there might be some errors in my code that I'm still not sure if they work correctly as the subtraction to calculate and join the current time with the previous one, tell me if it's right and if it is not correct correct me or do better. Thank you
Use microtime which returns the current Unix timestamp with microseconds
$game = [];
$game['start'] = microtime(true);
// Do stuff
sleep(3); // Without the sleep, start and end are the 'same'
$game['end'] = microtime(true);
$elapsedTime = ($game['end'] - $game['start']);
$minutes = floor($elapsedTime / 60);
$seconds = $elapsedTime % 60;
$milliseconds = number_format($elapsedTime - floor($elapsedTime),3);
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.
Hi I get an error when trying to get date interval using php strtotime function
the code is:
<?php
$interval = time() - strtotime('1992/08/13');
//expect to be 18
// but the output is 1988
print date('Y', $interval);
?>
any advice?
thanks
If you want to deal with date intervals in PHP I can't recommend the DateInterval class enough. I wrote a blog post on this earlier this week: Working with Date and Time in PHP
There's an example of using it there that should allow you to do what you want to do.
That is because all time() functions are seconds since epoch which is in 1970, so your out is actually 18 years since epoch. If you want it to get the difference in years you will probably have to calculate the difference yourself.
print $interval / (60*60*24*365.242199);
Are you tring to get the years elapsed rather than the actual year?
If so:
$year = 31556926;
$interval = time() - strtotime('1992/08/13');
print round($interval / $year);
$interval = time() - strtotime('1992/08/13');
These PHP functions deal with UNIX timestamps. That means the number of seconds from 1970. 01. 01. So 1992/08/13 is transformed into a timestamp (seconds). time() gives the current timestamp (seconds). You subtract the former from the latter, and you get the amount of seconds between those two dates. This is not a date itself, just an interval.
If you want to get the year, do something like echo $interval/(60*60*24*365); which will convert your seconds to years (not accurate, leap years will not be taken into consideration). Though your best option is checking out #James C's link and use his solutions. I just wanted to give some explanation.