简体   繁体   English

PHP的strtotime和工作多年?

[英]php strtotime and working out years?

I have a date stored in my mysql database and I can workout the days since the date like so, 我有一个存储在mysql数据库中的日期,我可以像这样从日期开始锻炼几天,

 ceil(time() - strtotime($account['joined']) / 60 / 60 / 24 % 365);

But how can I work out the years, since years is the highest format of time, since after that it is millenia and such? 但是,如何确定年份,因为年份是时间的最高格式,因此之后就是千年呢?

The variable is one such as: 变量是一个,例如:

2011-10-06 22:01:57

Thanks 谢谢

You can use the mysql database to get the timespan. 您可以使用mysql数据库获取时间跨度。

You could use DATEDIFF in your query to substract the joined-date from the current date and you can get the timespan this way. 您可以在查询中使用DATEDIFF从当前日期中减去加入日期,这样您就可以获得时间跨度。

This function is explained here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff 此功能的解释如下: http : //dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

Hope it helps 希望能帮助到你

You always can use date("Y", $timestamp) , 您始终可以使用date("Y", $timestamp)

and for your issue you can use date_diff, 对于您的问题,您可以使用date_diff,

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Use date("Y") 使用date("Y")

<?php 
$time = strtotime("1 Jan 2001 1:11:11"); 
echo "Years since time: ".(date("Y") - date("Y", $time));

http://sandbox.phpcode.eu/g/220fe/1 http://sandbox.phpcode.eu/g/220fe/1

If you can perform the calculation from the MySQL side, use the built-in date/time functions . 如果可以从MySQL端执行计算,请使用内置的日期/时间函数 For example assuming that table T contains a date column D and you want to compare it against the current date in terms of number of days: 例如,假设表T包含日期列D,并且您想将其与当前日期进行比较(以天数为单位):

SELECT 
    TIMESTAMPDIFF(DAY, D, CURDATE()) AS 'days_elapsed'
FROM T

You can change the first argument in TIMESTAMPDIFF to any of the following: FRAC_SECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR. 您可以将TIMESTAMPDIFF中的第一个参数更改为以下任意值:FRAC_SECOND(微秒),SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTER或YEAR。

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

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