943,660 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2223
  • PHP RSS
Nov 29th, 2008
0

Getting Future Date

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
xarz is offline Offline
24 posts
since Nov 2008
Nov 29th, 2008
0

Re: Getting Future Date

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...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is online now Online
6,654 posts
since Dec 2004
Nov 29th, 2008
0

Re: Getting Future Date

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.
php Syntax (Toggle Plain Text)
  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. ?>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Dec 1st, 2008
0

Re: Getting Future Date

@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..
Reputation Points: 10
Solved Threads: 1
Newbie Poster
xarz is offline Offline
24 posts
since Nov 2008
Dec 2nd, 2008
0

Re: Getting Future Date

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

PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 11
Solved Threads: 12
Junior Poster in Training
sikka_varun is offline Offline
94 posts
since Dec 2008
Dec 2nd, 2008
0

Re: Getting Future Date

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:

php Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
xarz is offline Offline
24 posts
since Nov 2008
Dec 9th, 2009
0

Getting Future date

Click to Expand / Collapse  Quote originally posted by xarz ...
Today is November 28, 2008

3 days from now is December 1,2008
Try below code it will give you future date after 3 days
PHP Syntax (Toggle Plain Text)
  1. echo date('l jS F (Y-m-d)', strtotime('+3 days'));
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chetan.akarte is offline Offline
1 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Upload Image With Resizing Done On Client
Next Thread in PHP Forum Timeline: Download and Approve





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC