[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

date

I am tryng to find a php script that will do the following "I have looked in the PHP manual and it just does not help me that much.

I want to be able to take a login in date and be able to calculate from that date to the 1st of the month following the coming month. That would be my start date and them calculate 1 year from the start date. I don't know if that make since or not.

If todays date was April 15, 2006, I would like the calculated to the 1st of June for the start date.
and the ending date would be one "1" year from 1st of june ending on the last day of May.

If some one could point me to a php script that might do this or show me how to do it I would highly appreciate it
0
starview
Asked:
starview
  • 7
  • 6
  • 4
1 Solution
 
BogoJokerCommented:
I will try to write you one right now, here is the logic I will test it out and give you some code soon.

1) Create date object with current date
2) Add one month to the date (dec --> jan special case)
3) Set day to 1
-- Start date is done --
4) Add one year to start date

Give me about 10 minutes to work this out.
Joe P
0
 
starviewAuthor Commented:
thank you
0
 
BogoJokerCommented:
Try this:
First run the code, make sure it is what you want.  The reason it is so bulky is because of those special cases but I can refine it a little I hope, its late, just hit 1:00 here so tell me how you think it is.

<?php
$today = getdate();
$newMonth = $today['mon'] + 2;
$year = $today['year'];
if ($newMonth > 12)
{
   $newMonth -= 12;
   $year++;
}
$startDate = mktime(0,0,0,$newMonth, 1, $year);
$year++;
$newMonth--;
if ($newMonth == 0)
{
   $newMonth = 12;
   $year--;
}
$tempEndDate = mktime(23,59,59, $newMonth, 28, $year);
$daysInMonth = date("t", $tempEndDate);
$endDate = mktime(23,59,59, $newMonth, $daysInMonth, $year);

// Print them out
$readTodayDate = date("n-j-Y", time());
print "Today's Date: $readTodayDate<br>";
$readStartDate = date("n-j-Y", $startDate);
print "Start Date: $readStartDate<br>";
$readEndDate = date("n-j-Y", $endDate);
print "End Date: $readEndDate<br>";
?>

Tested and it looks to me like that is exactly what you want.  Down to the second with time() notice it starts on 0hour 0 minute 0 second and ends 1 second away from a whole year from that moment, therefore 23 hour 59 minutes 59 seconds.
Joe P
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TeRReFCommented:
If you are using a DB to store stuff, it might be better to let the DB do the calculations for you. If you do, let me know and I'll show you a query that does the trick...
0
 
starviewAuthor Commented:
This worked great.
Yes, I will be using mysql database
0
 
TeRReFCommented:
Don't forget to close the question :)
0
 
starviewAuthor Commented:
You said you had a query that does the trick?
0
 
TeRReFCommented:
Oh, sorry. Your answer gave me the idea that you figured out the solution already. I'll have a look tomorrow, I really have to go to bed now :)
0
 
BogoJokerCommented:
How is the data stored in your database.
In a Date Datetime Timestamp ?
0
 
starviewAuthor Commented:
datetime
0
 
starviewAuthor Commented:
I did figure it out and I want to thank you for all you help!!!
0
 
BogoJokerCommented:
Alrighty =)
Good Work
0
 
TeRReFCommented:
Euh... so didn't you give the points to the wrong expert then?
0
 
starviewAuthor Commented:
No, go back and look. BogoJoker gave me the the script that show me what I ask for, please tell me what you really did?, the above speaks for it else. If I am wrong I am sorry.
0
 
TeRReFCommented:
Your sentence "Yes, I will be using mysql database".. gave me the idea that you were going for, and implemented the query solution (which is the better one in my opinion, it since it saves you al the PHP code and it will be faster). I misunderstood, my mistake..
0
 
starviewAuthor Commented:
Have a great day
0
 
TeRReFCommented:
Thanks, you too. Happy coding.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now