datediff

How to calculate age in T-SQL with years, months, and days

孤街醉人 提交于 2019-11-25 23:15:32
问题 What would be the best way to calculate someone\'s age in years, months, and days in T-SQL (SQL Server 2000)? The datediff function doesn\'t handle year boundaries well, plus getting the months and days separate will be a bear. I know I can do it on the client side relatively easily, but I\'d like to have it done in my stored procedure. 回答1: Here is some T-SQL that gives you the number of years, months, and days since the day specified in @date. It takes into account the fact that DATEDIFF()

What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?

こ雲淡風輕ζ 提交于 2019-11-25 22:45:25
问题 I\'m building a quick csv from a mysql table with a query like: select DATE(date),count(date) from table group by DATE(date) order by date asc; and just dumping them to a file in perl over a: while(my($date,$sum) = $sth->fetchrow) { print CSV \"$date,$sum\\n\" } There are date gaps in the data, though: | 2008-08-05 | 4 | | 2008-08-07 | 23 | I would like to pad the data to fill in the missing days with zero-count entries to end up with: | 2008-08-05 | 4 | | 2008-08-06 | 0 | | 2008-08-07 | 23 |

Calculate relative time in C#

不问归期 提交于 2019-11-25 21:41:46
问题 Given a specific DateTime value, how do I display relative time, like: 2 hours ago 3 days ago a month ago 回答1: Jeff, your code is nice but could be clearer with constants (as suggested in Code Complete). const int SECOND = 1; const int MINUTE = 60 * SECOND; const int HOUR = 60 * MINUTE; const int DAY = 24 * HOUR; const int MONTH = 30 * DAY; var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks); double delta = Math.Abs(ts.TotalSeconds); if (delta < 1 * MINUTE) return ts.Seconds == 1 ?

How to calculate the difference between two dates using PHP?

你。 提交于 2019-11-25 21:32:54
问题 I have two dates of the form: Start Date: 2007-03-24 End Date: 2009-06-26 Now I need to find the difference between these two in the following form: 2 years, 3 months and 2 days How can I do this in PHP? 回答1: Use this for legacy code (PHP < 5.3). For up to date solution see jurka's answer below You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it's rather easy to calculate different time periods. $date1 = "2007-03-24";