简体   繁体   中英

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,

 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.

You could use DATEDIFF in your query to substract the joined-date from the current date and you can get the timespan this way.

This function is explained here: 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) ,

and for your issue you can use 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")

<?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

If you can perform the calculation from the MySQL side, use the built-in date/time functions . 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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