Page 1 of 1

implementing time into PHP

Posted: Wed May 21, 2014 9:26 pm
by kierongi
Hello Guys

and thanks for you time


I was just wondering if there was a way to implement time into PHP code

lets say that i am playing a game i which I need to go to the shops to get supplies.

instead of instantly going to the shop and retrieving the supplies i want it to take me 5 mins

for example

user goes to the shop -> wait 5 mins -> results

-------------------------
sorry if i am making no sense just a general query and if so a little help on how to get this working if that is allowed here

Thanks

Re: implementing time into PHP

Posted: Thu May 22, 2014 4:56 am
by Xaleph
Well, it all comes to down to how you implement it. You should have stored a time value somewhere ( which you can use as reference ) Im guessing you`re using mysql(i) so ì`m guessing either DateTime or timestamp. Either is fine because PHP has some nice functions for both.

There used to be a whole function sheet for time, but as of .. i don`t know, version 5.2 (?) you can use the Datetime Object, which is pretty awesome.

In PHP you can use it like this:

$sometime = new Datetime();

If you want to give it a specific time ( from your database for example ) you can just pass it in the constructor like so:

$sometime = new Datetime( $stored_time_value_from_database );

Now, you can use all kinds of cool methods, like diffing time!

So you have the current time ( using an empty $current_time = new Datetime(); ) and you can use it to differentiate from your database time like this:

$now = new Datetime();
$stored = new Datetime ( $value_from_database );
$difference = $now->diff ( $stored );

And you get an object which has all the time differences you need! Good luck