Forum Moderators: coopster

Message Too Old, No Replies

PHP Date - calculating periods

         

adammc

6:41 am on Aug 21, 2007 (gmt 0)

10+ Year Member



Hi Guys,

How would I get the next 2 week period (closest to current date)?
Periods - 1st - 15th each month, 16th - end of month.

[php]
$now = time();
$currentDate = date("Y-m-d H:i:s", $now);

$previous_period = ? // Y-m-d H:i:s
$next_period= ? // Y-m-d H:i:s
[/php]

example output would be:
today = 21st August 2007
Prev Period = 15th August 2007
Next Period = 1st September

dreamcatcher

6:49 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try looking at strtotime [php.net].

dc

adammc

7:08 am on Aug 21, 2007 (gmt 0)

10+ Year Member



Hi dreamctacher!

I have, still no clue :(

I can get the pay period using:

if ($thisDay < 15)
{
$nextPayPeriod = "15th"; // of this month


} else {
$nextPayPeriod = "1st"; // of next month

}

But how would I get the exact date / time for the next and previous periods?

Habtom

7:13 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about just getting the values separately this way:

$thisDay = date("d", time());
$thismonth = date("m", time()) + 0;
$nextmonth = date("m", time()) + 1;

. . . and use them in your if conditions.

Habtom

Habtom

7:20 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.. . if you want the month in extended format :)

$thisDay = date("d", time());
$thismonth = date("F", time());
$nextmonth = date("F", strtotime("next month", time()));

Habtom

dreamcatcher

8:08 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or:

$today = date("Y-m-d H:i:s");

//minus 2 weeks..
$prev = date("Y-m-d H:i:s", strtotime("-2 weeks"));

//plus 2 weeks..
$next = date("Y-m-d H:i:s", strtotime("+2 weeks"));

Or am I missing something?

dc

Habtom

8:17 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or am I missing something?

A little I guess, I think he wanted them to be from the begining of the month, and the first day of the month. 2 weeks might roughly be thre, but I am not sure if that is what he is looking for.

adammc

10:14 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



thanks guys :)

Waht I was after was:

If today was the 21st August 2007 I want to set 2 variables:

period_start_date = 15th August 2007
period_end_date = 31st August 2007


If today was the 2nd Sept 2007 I want to set 2 variables:

period_start_date = 1st Sept 2007
period_end_date = 14th sep 2007

adammc

10:34 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



Would this work?

[php]
// Set date / time variables
$thisMonth = date("F", $now); // example - August
$next_Month = date("F", strtotime("+1 months")); // example - September
$thisYear = date("Y", $now); // example - 2007

// get pay period and run query
if ($thisDay < 15)
{
$PayStartDay= "01"; // day of this month
$PayEndDay = "14"; // day of this month

$query = " select * from tablename WHERE sale_date BETWEEN $PayStartDay - $thisMonth - $thisYear ";
// run rest of code here


} else {

$PayStartDay= "15"; // day of this month
$PayEndDay = "31"; // day of this month

$query = " select * from tablename WHERE sale_date BETWEEN $PayStartDay - $thisMonth - $thisYear ";
// run rest of code here

}
[/php]