0

I am truely sorry if this question is already answered. If so, please redirect me to the correct thread or website. I didn't manage to find the question and answer i am looking for.

So here is my question. I am thinking about building a browser game and i am fairly known with PHP and MySQL. But my only trouble (for now) is with timers.

What i want to do:

User presses a button on a web page to (lets say) level up a building or whatever. The level up progress takes 2 hours. I did manage to get the current time in PHP and i think i can manage to add two hours to that time. So i can store it in the database. But my questions are:

  • How do i format the PHP date and time to be suitable for MySQL? (i've seen alot about strtotime() and methods like that, but i cant seem to get the hang of it because it doesnt really solve my problem)
  • What type of column should i use? There are many types, like datetime, timestamp etc..
  • On page refresh i want to fetch the timestamp from the database and compare it with the current time. So i can see if it already expired or not. If not i display how many days, hours, minutes or seconds are left. (I dont need code for a real count down timer, that is stuff for when i am really going to code).

This is also going to be used for things like resources and attacks. But first i want to know if this is even possible in the way i am thinking. If not or if there is a better way, like doing it in ruby, python or any other language i would love to know what and where is should look into. I am willing to learn other languages to do this.

Now i dont really need (if it's done, i would dearly appreciate it ofcourse) any of you to write it for me. Information on what and where to look would already be much appreciated.

Any help would be much appreciated!

Cheers,

Clemenz

Fjarlaegur
  • 1,395
  • 1
  • 14
  • 33

1 Answers1

2

I use DateTime class to format date most of the time.

$date = new DateTime('NOW'); //'NOW' could be datetime from database
$formattedDate = $date->format("M j, Y, g:i A");

And you can get the difference between the time using:

$d1=new DateTime("2012-07-09 11:14:15");
$d2=new DateTime("2012-07-08 11:14:15");
$diff=$d2->diff($d1);
print_r( $diff ) ;

Reference for class DateTime: http://php.net/manual/en/class.datetime.php

You can use the columns in the database as said by @DaveChen

Konsole
  • 3,447
  • 3
  • 29
  • 39
  • If the columns are using [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601), then you can sort without using any functions with the database. +1 – Dave Chen Jul 23 '13 at 01:48