I'd like my user to be able to input values like:
4 hours
23 minutes
etc.
strtotime works great for converting these values into seconds, but it adds them to the current time. Is there a way of getting it to return the quantity of time entered in total, rather than from now? Or do I need to do something like this:
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
$time = strtotime($value, 0);
http://php.net/strtotime
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
Then you will miss some seconds (probably 1), because strtotime($value) is evaluated first and if the time goes by the result of time() will be bigger then expected (1 second probably ;))
You can make sure you're always working with the same time using this pattern:
$start = time();
$time = strtotime($value, $start) - $start;
Or even easier, skip the subtraction by setting the second argument to 0:
$time = strtotime($time, 0);
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 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);
With PHP, I am trying to convert a bunch of numbers into a a readable format, the thing is, I have no idea how/what format these are in or can be parsed in using the date() or time() functions in php. there are two of these as well.
(they're built from a total time spent online and time since last log-on)
onlinetime : 1544946 = 2w 3d 21h 9m
lastonline : 1397087222 = 1h 32m
does anyone know the way to get the two different times from the two different timestamps?
If you have a Unix timestamp, take a look at Convert timestamp to readable date/time PHP. The PHP documentation is here: http://php.net/manual/en/function.date.php.
For the online time, you could do modulo arithmetic to figure out the values for each, and then just make a string out of the result. Someone may have a nicer suggestion for this though.
I think John is right, the first is the number of seconds in the timespan listed. And the second certainly looks like a unix timestamp to me. So here's how you can get what you want from these sets of numbers:
1) For the first number, simply divide the number by the seconds in a given time span and use floor():
$timeElapsed = 154496; // in this case
$weeksElapsed = floor($timeElapsed / 604800);
$remainder = $timeElapsed % 604800;
$daysElapsed = floor($remainder / 86400);
etc...
2) For the second number, you can do the same thing by first getting the current timestamp and then subtracting the given timestamp from it:
$lastOnline = 1397087222; // again, in this case
$currentTimestamp = time();
$elapsedSinceLastLogin = $currentTimestamp - $lastonline;
$weeksSinceLastLogin = floor($elapsedSinceLastLogin / 604800);
etc...
I have a file called "file.txt".
I get its last change time with this:
$lastTime = filemtime("file.txt");
Then I make
$lastDate = date("Y-m-d",strtotime( $lastTime ));
$todaysDate = date("Y-m-d",strtotime('now'));
Then I substract lastDate from todaysDate and find the difference.
Is there a quicker way to do this?
I don't want to check whether the difference of seconds is >= 86400 or not.
For example the difference between these two dates must be "1".
2013-03-31 10:00
2013-03-30 19:00
If i check for the difference of seconds I will have 54K seconds, which is smaller than 86400 seconds.
Get the difference in seconds, then convert to days and round the result.
$diff = time()-filemtime("file.txt");
$days = round($diff/86400);
You could also use floor or ceil in place of round depending on exactly how you want to handle partial days.
Hey guys i'm trying to figure out how to subtract one time from another using php to get the amount of time left between the two times. So for example
time left = time1-time2
or
timeleft = 15:35-15:30
which would be equal to 5mins left.
Currently I am loading the two times like so.
time1 is coming from my database (which is the time we are waiting for, and in my case the time we are waiting for is the time for next update) and time2 is the current system time.
I tried using this code
$timeleft = $dbtime - $curtime;
$dbtime = time loaded from database.
$curtime = current system time.
But that just returns a 0.
Any help is appreciated thanks.
Use strtotime to turn the date string to unix timestamp.
$timeleft = strtotime($dbtime) - strtotime($curtime);
You have to convert both times into timestamp. One good function for that is the strtotime() http://php.net/manual/en/function.strtotime.php that try to convert a string into timestamp.
Then do your maths as you know and then just use the date() http://www.php.net/manual/en/function.date.php function to fomrat your time into anything you like
Use strtotime to convert strings to unix timestamps:
$timedifference = strtotime($dbtime) - strtotime($curtime); // or also
$timedifference = strtotime($dbtime) - time();
You negating one string from another - the result in 0 because (int)(string) = 0
Your must use like this
$dbtime = time();
// query
$timeleft = time() - $dbtime;
See also strtotime() function, if you date is parsed by it