简体   繁体   English

此“日期”功能的帮助很小

[英]Little help with this “date” function

I have this to select a date from a mysql db, and compare it to an array of month-names in swedish language. 我要从mysql数据库中选择一个日期,并将其与瑞典语的月份名称数组进行比较。

$monthnames = array("","Januari","Februari","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December");

$postdate = $monthnames[date("n", strtotime( $row['modify_date'] ))];
//Outputs something like '12 Februari'

Here is the prob, I want to check the $postdate variable and change it to "Today", "Yesterday" and "Day before yesterday" according to the date, how can I do so? 这是一个问题,我想检查$ postdate变量,并根据日期将其更改为“ Today”,“ Yesterday”和“昨天之前”,该怎么办?

Thanks 谢谢

If you're using timestamps to store dates in the database: 如果您使用时间戳记将日期存储在数据库中:

You can have preset intervals like: 您可以设置以下预设间隔:

$day = mktime(0,0,0,2,1,2001)-mktime(0,0,0,1,1,2001);

And you can compare the time now - timestamps/dates you have in the database with those intervals. 您可以现在比较时间-数据库中的时间戳记/日期以及这些时间间隔。

For example, let's say $dbTime contains a timestamp fetched with mysql: 例如,假设$ dbTime包含一个用mysql获取的时间戳:

$time = time() - $dbTime;
if($time<$day)
   echo 'posted today';
elseif($time<($day*2))
   echo 'posted yesterday';

etc etc. 等等等

Then you can use PHP's Date function to echo hrs, minutes, secs, or dates, if they are larger than your predefined intervals. 然后,如果小时,分钟,秒或日期大于预定义的间隔,则可以使用PHP的Date函数来回显它们。

There is no such in-built function in PHP. PHP中没有这样的内置函数。 You got to do calculations, the mktime function can be your friend in this case. 您必须进行计算,在这种情况下, mktime函数可以成为您的朋友。

if (date of $timenow == date of $timepost) { today; }
else if (date of $timenow - 24h == date of $timepost) { yesterday; }
etc.

Be aware about summer/winter time change, timezones etc. so try to use mktime() 请注意夏/冬时间更改,时区等,因此请尝试使用mktime()

http://php.net/date http://php.net/date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM