duanbei1903 2013-07-23 01:23
浏览 35

PHP和MYSQL游戏计时器比较[关闭]

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

  • 写回答

1条回答 默认 最新

  • douzhuanqian8244 2013-07-23 01:44
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。