Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Linux, Apache, MySQL > MySQL Help

MySQL Help Post any question relating to MySQL here and hopefully someone can help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 2006-02-23, 11:43 AM
Junior Member
 
Join Date: Feb 2006
Posts: 1
guilty_spark
Default

I'm having difficulty with mySQL date operations.

I have a start date (in table s_emp), and need to subtact this from today's date in order to formulate a 'length of service' value in years.

Any help?

Thanks

-- GS --
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2006-03-17, 09:35 AM
Member
 
Join Date: May 2003
Posts: 47
barand
Default

Something like

Code:
SELECT name, (TO_DAYS(CURDATE()) - TO_DAYS(start_date))/365 AS service_years FROM s_emp
ORDER BY service_years DESC
__________________
Barand
Reply With Quote
  #3 (permalink)  
Old 2007-10-22, 08:48 AM
Junior Member
 
Join Date: Oct 2007
Posts: 22
celon_php_master is on a distinguished road
Default

it is easy

let say ur database table date filed is in the timestamp fromat

$Today_date=date("Y-m-d");

$D1=substr($table_date_field,0,8);
$D2=substr($Today_date,0,8);

$Date_str_one=strtotime($D1);
$Date_str_two=strtotime($D2);
$no_of_dayz= ($Date_str_two-$Date_str_one)/(3600*24);
Reply With Quote
  #4 (permalink)  
Old 2007-11-30, 01:12 AM
Junior Member
 
Join Date: Nov 2007
Location: San Diego, CA
Posts: 2
danielsonchris is on a distinguished road
Default

Quote:
Originally Posted by barand View Post
Something like

Code:
SELECT name, (TO_DAYS(CURDATE()) - TO_DAYS(start_date))/365 AS service_years FROM s_emp
ORDER BY service_years DESC
The above code does not take into account leap years. You will want to use the DateDiff function or the DateAdd function. The DateAdd function allows for you to pass an "interval" in as a parameter. This will allow the function to perform a subtraction routine.

Regards,
Chris Danielson
__________________
MaxPowerSoft, LLC

Last edited by danielsonchris; 2007-11-30 at 01:16 AM. Reason: End user wanted result in years not days.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.0 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.0 (a popular PHP IDE) and NuCoder 1.4 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:30 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.