Re: [PHP] Date Math
On 20 April 2010 17:40, Floyd Resler fres...@adex-intl.com wrote: I need to get the difference in months between two dates. The dates could be as much as 60 months apart. Is there any easy way to do this either through PHP or MySQL? I know how I can do it through code but thought there might be a simple one or two line option. Thanks! Floyd You're best off doing this in MySQL. Something like select timestampdiff(month, '2010-01-01', '2010-05-22'); should work. Michiel
Re: [PHP] Date Math
On Apr 21, 2010, at 5:39 AM, Michiel Sikma wrote: On 20 April 2010 17:40, Floyd Resler fres...@adex-intl.com wrote: I need to get the difference in months between two dates. The dates could be as much as 60 months apart. Is there any easy way to do this either through PHP or MySQL? I know how I can do it through code but thought there might be a simple one or two line option. Thanks! Floyd You're best off doing this in MySQL. Something like select timestampdiff(month, '2010-01-01', '2010-05-22'); should work. Michiel Perfect! That's exactly what I was looking for! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date Math
On Tue, Apr 20, 2010 at 11:40 AM, Floyd Resler fres...@adex-intl.comwrote: I need to get the difference in months between two dates. The dates could be as much as 60 months apart. Is there any easy way to do this either through PHP or MySQL? I know how I can do it through code but thought there might be a simple one or two line option. check out: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html -- -Dan Joseph www.canishosting.com - Unlimited Hosting Plans start @ $3.95/month. Promo Code NEWTHINGS for 10% off initial order http://www.facebook.com/canishosting http://www.facebook.com/originalpoetry
Re: [PHP] Date Math
At 11:40 AM -0400 4/20/10, Floyd Resler wrote: I need to get the difference in months between two dates. The dates could be as much as 60 months apart. Is there any easy way to do this either through PHP or MySQL? I know how I can do it through code but thought there might be a simple one or two line option. Thanks! Floyd ?php $date1 = '2009-02-27'; $date2 = '2004-12-03'; $udate1 = strtotime($date1); $udate2 = strtotime($date2); $difference = $udate1 - $udate2; $months_difference = floor($difference / 2678400); echo(The difference is $months_difference months); ? I this will work, but the question of what constitutes a month might come into play. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date Math
On Tue, Apr 20, 2010 at 03:32:58PM -0400, tedd wrote: At 11:40 AM -0400 4/20/10, Floyd Resler wrote: I need to get the difference in months between two dates. The dates could be as much as 60 months apart. Is there any easy way to do this either through PHP or MySQL? I know how I can do it through code but thought there might be a simple one or two line option. Thanks! Floyd ?php $date1 = '2009-02-27'; $date2 = '2004-12-03'; $udate1 = strtotime($date1); $udate2 = strtotime($date2); $from = getdate($udate1); $from_year = $from['year']; $from_month = $from['month']; $to = getdate($udate2); $to_year = $to['year']; $to_month = $to['month']; // Assumes $to_date is later than $from_date if ($from_year == $to_year) $months = $to_month - $from_month; elseif ($to_month = $from_month) { $num_years = $to_year - $from_year; $add_months = $num_years * 12; $base_months = $to_month - $from_month; $months = $base_months + $add_months; } else { // $to_month $from_month $num_years = $to_year - $from_year; $base_months = $num_years * 12; $sub_months = $from_month - $to_month; $months = $base_months - $sub_months; } This will give the months between two dates, ignoring the actual day of the month for each date (may not be exactly what you want, and code is untested). $difference = $udate1 - $udate2; $months_difference = floor($difference / 2678400); echo(The difference is $months_difference months); ? I this will work, but the question of what constitutes a month might come into play. My code above is submitted because I don't like doing calculations with seconds. Tedd's right, the OP's questions can't be answered precisely because months vary in number of days. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date math
On Sun, March 23, 2008 11:17 pm, Ron Piggott wrote: I have this math equation this list helped me generate a few weeks ago. The purpose is to calculate how many days have passed between 2 dates. Right now my output ($difference) is 93.958333 days. I am finding this a little weird. Does anyone see anything wrong with the way this is calculated: $date1 = strtotime($date1); (March 21st 2008) $date2 = strtotime($date2); (December 18th 2007) echo $date1 = 1206072000 echo $date2 = 1197954000 #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); float division introduces rounding errors, by the nature of a floating point representation in a finite number of bits. Use http://php.net/round and http://php.net/int -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date math
Could someone then help me modify the PHP script so I won't have this timezone issue? I don't understand from looking at the date page on the PHP web site the change(s) I need to make. Thanks, Ron ? $date1 = strtotime($date1); $date2 = strtotime($date2); $factor = 86400; $difference = (($date1 - $date2) / $factor); On Mon, 2008-03-24 at 07:24 -0700, Jim Lucas wrote: Ron Piggott wrote: I have this math equation this list helped me generate a few weeks ago. The purpose is to calculate how many days have passed between 2 dates. Right now my output ($difference) is 93.958333 days. I am finding this a little weird. Does anyone see anything wrong with the way this is calculated: $date1 = strtotime($date1); (March 21st 2008) $date2 = strtotime($date2); (December 18th 2007) echo $date1 = 1206072000 echo $date2 = 1197954000 #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); As Casey suggested, it is a timestamp issue. Checkout my test script. http://www.cmsws.com/examples/php/testscripts/[EMAIL PROTECTED]/0001.php plaintext?php $date1 = strtotime('March 21st 2008'); //(March 21st 2008) echo date1 = {$date1}\n; $date2 = strtotime('December 18th 2007'); //(December 18th 2007) echo date2 = {$date2}\n; $date1 = 1206072000; echo date('c', $date1).\n; $date2 = 1197954000; echo date('c', $date2).\n; #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); echo $difference.\n; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date math
(top-posting!) Add either the round function or ceil function. On Mar 25, 2008, at 6:47 AM, Ron Piggott [EMAIL PROTECTED] wrote: Could someone then help me modify the PHP script so I won't have this timezone issue? I don't understand from looking at the date page on the PHP web site the change(s) I need to make. Thanks, Ron ? $date1 = strtotime($date1); $date2 = strtotime($date2); $factor = 86400; $difference = (($date1 - $date2) / $factor); On Mon, 2008-03-24 at 07:24 -0700, Jim Lucas wrote: Ron Piggott wrote: I have this math equation this list helped me generate a few weeks ago. The purpose is to calculate how many days have passed between 2 dates. Right now my output ($difference) is 93.958333 days. I am finding this a little weird. Does anyone see anything wrong with the way this is calculated: $date1 = strtotime($date1); (March 21st 2008) $date2 = strtotime($date2); (December 18th 2007) echo $date1 = 1206072000 echo $date2 = 1197954000 #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); As Casey suggested, it is a timestamp issue. Checkout my test script. http://www.cmsws.com/examples/php/testscripts/[EMAIL PROTECTED]/0001.php plaintext?php $date1 = strtotime('March 21st 2008'); //(March 21st 2008) echo date1 = {$date1}\n; $date2 = strtotime('December 18th 2007'); //(December 18th 2007) echo date2 = {$date2}\n; $date1 = 1206072000; echo date('c', $date1).\n; $date2 = 1197954000; echo date('c', $date2).\n; #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); echo $difference.\n; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date math
Ron Piggott wrote: I have this math equation this list helped me generate a few weeks ago. The purpose is to calculate how many days have passed between 2 dates. Right now my output ($difference) is 93.958333 days. I am finding this a little weird. Does anyone see anything wrong with the way this is calculated: $date1 = strtotime($date1); (March 21st 2008) $date2 = strtotime($date2); (December 18th 2007) echo $date1 = 1206072000 echo $date2 = 1197954000 #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); As Casey suggested, it is a timestamp issue. Checkout my test script. http://www.cmsws.com/examples/php/testscripts/[EMAIL PROTECTED]/0001.php plaintext?php $date1 = strtotime('March 21st 2008'); //(March 21st 2008) echo date1 = {$date1}\n; $date2 = strtotime('December 18th 2007'); //(December 18th 2007) echo date2 = {$date2}\n; $date1 = 1206072000; echo date('c', $date1).\n; $date2 = 1197954000; echo date('c', $date2).\n; #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); echo $difference.\n; -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date math
On 24/03/2008, at 5:17, Ron Piggott wrote: I have this math equation this list helped me generate a few weeks ago. The purpose is to calculate how many days have passed between 2 dates. Right now my output ($difference) is 93.958333 days. I am finding this a little weird. Does anyone see anything wrong with the way this is calculated: $date1 = strtotime($date1); (March 21st 2008) $date2 = strtotime($date2); (December 18th 2007) echo $date1 = 1206072000 echo $date2 = 1197954000 #86400 is 60 seconds x 60 minutes x 24 hours (in other words 1 days worth of seconds) $factor = 86400; $difference = (($date1 - $date2) / $factor); Depending on what you want, wrap it in round() ceil() or floor() -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- Simon Welsh Admin of http://simon.geek.nz/ Windows is a joke operating system. Hell, it's not even an operating system. NT is Not Tough enough for me either. 95 is how may times it will crash an hour. http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date math
On Sun, Mar 23, 2008 at 9:17 PM, Ron Piggott [EMAIL PROTECTED] wrote: I have this math equation this list helped me generate a few weeks ago. The purpose is to calculate how many days have passed between 2 dates. snip $date1 = strtotime($date1); (March 21st 2008) $date2 = strtotime($date2); (December 18th 2007) echo $date1 = 1206072000 echo $date2 = 1197954000 Seems to be a time zone issue.1206057600 is the actual timestamp for March 21st, 2008 GMT. I don't know what time zone 1206072000 is is. -- -Casey -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] date math question
This leads me to another question. If I have stored the date as an epoch then is there a way using PHP and MySQL to say find all the records that have been added this YEAR (not last 365 days)? SELECT * FROM table WHERE YEAR(FROM_UNIXTIME(column)) = YEAR(CUR_DATE()); Benchmark each method and see which is faster... ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] date math question
On March 27, 2003 09:15 pm, Charles Kline wrote: I am storing my dates as unix timestamp (epoch). Am I right in assuming that if I need to add or subtract days from this it is done in seconds? yes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] date math question
I think there's problems doing that when daylight savings starts/ends Just something to keep in mind... -Original Message- From: Leo Spalteholz [mailto:[EMAIL PROTECTED] Sent: Friday, March 28, 2003 4:32 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] date math question On March 27, 2003 09:15 pm, Charles Kline wrote: I am storing my dates as unix timestamp (epoch). Am I right in assuming that if I need to add or subtract days from this it is done in seconds? yes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] date math question
Okay cool. This leads me to another question. If I have stored the date as an epoch then is there a way using PHP and MySQL to say find all the records that have been added this YEAR (not last 365 days)? Thanks Charles On Friday, March 28, 2003, at 12:31 AM, Leo Spalteholz wrote: On March 27, 2003 09:15 pm, Charles Kline wrote: I am storing my dates as unix timestamp (epoch). Am I right in assuming that if I need to add or subtract days from this it is done in seconds? yes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] date math question
hi, $start = mktime ( 0, 0, 0, 1, 1, date['Y']); // first day of this year $end = mktime ( 0, 0, 0, 12, 31, date['Y']); // last day of this year then select all record where timestamp = $start and timestamp = $end should do the job Hope this helps Foong Charles Kline [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Okay cool. This leads me to another question. If I have stored the date as an epoch then is there a way using PHP and MySQL to say find all the records that have been added this YEAR (not last 365 days)? Thanks Charles On Friday, March 28, 2003, at 12:31 AM, Leo Spalteholz wrote: On March 27, 2003 09:15 pm, Charles Kline wrote: I am storing my dates as unix timestamp (epoch). Am I right in assuming that if I need to add or subtract days from this it is done in seconds? yes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] date math question
sorry typo error should be: date('Y') Foong Foong [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] hi, $start = mktime ( 0, 0, 0, 1, 1, date['Y']); // first day of this year $end = mktime ( 0, 0, 0, 12, 31, date['Y']); // last day of this year then select all record where timestamp = $start and timestamp = $end should do the job Hope this helps Foong Charles Kline [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Okay cool. This leads me to another question. If I have stored the date as an epoch then is there a way using PHP and MySQL to say find all the records that have been added this YEAR (not last 365 days)? Thanks Charles On Friday, March 28, 2003, at 12:31 AM, Leo Spalteholz wrote: On March 27, 2003 09:15 pm, Charles Kline wrote: I am storing my dates as unix timestamp (epoch). Am I right in assuming that if I need to add or subtract days from this it is done in seconds? yes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] date math question
Caught that :) Thanks for the tip... worked just perfect (after I fixed typo) On Friday, March 28, 2003, at 12:57 AM, Foong wrote: sorry typo error should be: date('Y') Foong Foong [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] hi, $start = mktime ( 0, 0, 0, 1, 1, date['Y']); // first day of this year $end = mktime ( 0, 0, 0, 12, 31, date['Y']); // last day of this year then select all record where timestamp = $start and timestamp = $end should do the job Hope this helps Foong Charles Kline [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Okay cool. This leads me to another question. If I have stored the date as an epoch then is there a way using PHP and MySQL to say find all the records that have been added this YEAR (not last 365 days)? Thanks Charles On Friday, March 28, 2003, at 12:31 AM, Leo Spalteholz wrote: On March 27, 2003 09:15 pm, Charles Kline wrote: I am storing my dates as unix timestamp (epoch). Am I right in assuming that if I need to add or subtract days from this it is done in seconds? yes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php