implementing time into PHP

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
kierongi
Posts: 30
Joined: Mon Aug 12, 2013 9:57 pm

implementing time into PHP

Post 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
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: implementing time into PHP

Post 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
Post Reply

Return to “Beginner Help and Support”