Getting Future Date

Thread Solved

Join Date: Nov 2008
Posts: 24
Reputation: xarz is an unknown quantity at this point 
Solved Threads: 1
xarz's Avatar
xarz xarz is offline Offline
Newbie Poster

Getting Future Date

 
0
  #1
Nov 29th, 2008
I don't know how to explain this correctly but just some sample for you guys so that you can really get what Im trying to say.

Today is November 28, 2008

3 days from now is December 1,2008

Im looking for a php code, which can give me the exact date giving the number of days interval prior to the current date.

I've been looking for a thread which can solve or even give a hint on how to solve this one but I found none.
:: xarz ::
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,181
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Getting Future Date

 
0
  #2
Nov 29th, 2008
Is it so difficult to use google with keywords search php+get+date+future.
From what I got from this search even me with no interest in PHP could make that part of the code work...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,438
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 134
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Getting Future Date

 
0
  #3
Nov 29th, 2008
I have created a script for you to fiddle with and is below. The below script is a function where you can enter the number of days, months and years untill the future date and the function will return what that date is.
  1. <?
  2. function count_to_date($day,$month,$year)
  3. {
  4. $d=date(j)+$day;
  5. $m=date(n)+$month;
  6. $y=date(Y)+$year;
  7. if ($d>31)
  8. {
  9. while ($d>31)
  10. {
  11. $d-=31;
  12. $m+=1;
  13. }
  14. }
  15. if ($m>12)
  16. {
  17. while ($m>12)
  18. {
  19. $m-=12;
  20. $y+=1;
  21. }
  22. }
  23. //below sets sequence: day/month/year
  24. $r=$d.'/'.$m.'/'.$y;
  25. return $r;
  26. }
  27.  
  28.  
  29. //below returns a future date (day/month/year)
  30. echo count_to_date(3,0,0);
  31.  
  32. //a bit of theory
  33. //count_to_date(number_of_days,number_of_months,number_of_years);
  34. ?>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: xarz is an unknown quantity at this point 
Solved Threads: 1
xarz's Avatar
xarz xarz is offline Offline
Newbie Poster

Re: Getting Future Date

 
0
  #4
Dec 1st, 2008
@cwarn23

great! thanks alot for the code..
i haven't tried it yet.. but will let you know if there are any problems with it..
:: xarz ::
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: Getting Future Date

 
0
  #5
Dec 2nd, 2008
Hi I really doubt whether this piece of code will work for every date.. It might not work with february date, leap years, and for months with months with 30 days like april..

Im providing you with some other code sample, that can work for most of the cases...

  1. <?php
  2. function count_to_date($curr_day,$curr_month,$curr_yr,$day,$month,$year)
  3. {
  4. $d=$curr_day+$day;
  5. $m=$curr_month+$month;
  6. $y=$curr_yr+$year;
  7. if ($m>12)
  8. {
  9. $m-=12;
  10. $y+=1;
  11. }
  12.  
  13. $no_of_days=get_days_in_month($m,$y);
  14. if ($d>$no_of_days)
  15. {
  16. $d-=$no_of_days;
  17. $m+=1;
  18. }
  19.  
  20. //below sets sequence: day/month/year
  21. $r=$d.'/'.$m.'/'.$y;
  22. return $r;
  23. }
  24.  
  25. function leap_year($yr)
  26. {
  27. if($yr%4==0 || $yr%400==0)
  28. return true;
  29. else
  30. return false;
  31. }
  32. function get_days_in_month($mnth_no,$yr)
  33. {
  34. switch($mnth_no)
  35. {
  36. case 1:
  37. case 3:
  38. case 5:
  39. case 8:
  40. case 10:
  41. case 12:return 31;
  42. break;
  43.  
  44. case 4:
  45. case 6:
  46. case 9:
  47. case 11:return 30;
  48. break;
  49.  
  50. case 2:if(leap_year($yr))
  51. return 29;
  52. else
  53. return 28;
  54. break;
  55. }
  56. }
  57.  
  58. //below returns a future date (day/month/year)
  59. echo count_to_date(30,3,2007,1,0,1);
  60.  
  61. //a bit of theory
  62. //count_to_date(number_of_days,number_of_months,number_of_years);
  63. ?>
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: xarz is an unknown quantity at this point 
Solved Threads: 1
xarz's Avatar
xarz xarz is offline Offline
Newbie Poster

Re: Getting Future Date

 
0
  #6
Dec 2nd, 2008
sikka_varun and cwarn23


thanks alot for the codes..

I make some modifications to fit my need from your work guys.
basically the changes are adding a parameter so that I can choose what date I want to start counting off.
Here is the outcome:

  1. function count_to_date($sqldate,$day,$month,$year)
  2. {
  3. $d=substr($sqldate,8,2);
  4. $m=substr($sqldate,5,2);
  5. $y=substr($sqldate,0,4);
  6. $h=substr($sqldate,11,2);
  7. $min=substr($sqldate,14,2);
  8.  
  9. $d=intval($d)+$day;
  10. $m=intval($m)+$month;
  11. $y=intval($y)+$year;
  12. if ($d>31)
  13. {
  14. while ($d>31)
  15. {
  16. $d-=31;
  17. $m+=1;
  18. }
  19. }
  20. if ($m>12)
  21. {
  22. while ($m>12)
  23. {
  24. $m-=12;
  25. $y+=1;
  26. }
  27. }
  28. //below sets sequence: day/month/year
  29. if($d<10) $d='0'.$d;
  30. if($m<10) $m='0'.$m;
  31. $r=$y.'-'.$m.'-'.$d.' '.$h.':'.$min.':00';
  32. return $r;
  33. }

can be called by
  1. echo count_to_date("2007-07-01 06:00:00",5,0,0);

and the output will be
2007-07-06 06:00:00

anyways,thanks alot for the suggestions and the sample code.. i really appreciate it.
Last edited by xarz; Dec 2nd, 2008 at 10:29 pm.
:: xarz ::
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC