开发者

Using unit constants

开发者 https://www.devze.com 2023-02-03 13:17 出处:网络
How do you feel about using unit constants such as: define(\"HOUR\", 3600); define(\"DAY\", 86400); define(\"YEAR\", 31556926);

How do you feel about using unit constants such as:

define("HOUR", 3600);
define("DAY", 86400);
define("YEAR", 31556926);
开发者_StackOverflow

... so that you can do things like:

cookie::set("key", "value", DAY);


Depends. If the constants are used often, they may warrant their existence. I also generally prefer a constant over some number, as it can be given a somewhat descriptive name.

But, for multiples of common time periods, I think stuff like the following is generally very well understood at a quick glance, and sometimes more concise than some overly verbose namespaced constant. I'm assuming its common knowledge of how many seconds are in a minute, hour, and day, which I think is a decent assumption.

60 * 45; //45 min
3600 * 8; // 8 hrs
86400 * 100; // 100 days
86400 * 365 * 5; // 5 yrs

But if the constants are there...I'd probably use them.


I do this a lot: so you don't have to re-type that "31556926", and maybe make a mistake.

(Apart from the fact that you've not defined MONTH :) )

0

精彩评论

暂无评论...
验证码 换一张
取 消