5

I am trying to figure out on how to set my dates to every 15th and end of month only...what i'm getting so far is only +15 days from my current date.. current date = date today.LOGIC: if the current day is less than 15 then the start date of the loop is on the end of the month then if the current day is equal or greater than 15 then the start date of the loop is on the 15th. so in my case my current date is Nov 9 so the First output should be Nov 30.

$y = 1;
while ($y <= $num_term) { // num_term is equal to the number of output
    $month_line = strtotime("15 day", strtotime("$month_sched"));
    $day = date("d", $month_line);
    $month_int = date("M-d", $month_line);
} 

Output:

Nov 24`<br />`
Dec 9`<br />`
Dec 24`<br />`

Could anybody help me... thanks :)

Peter
  • 8,776
  • 6
  • 62
  • 95
zuma
  • 93
  • 3
  • 15

5 Answers5

2

Try this

  1. Find the first day of month
  2. Find the Last Day of month as Lalji Nakum told
  3. Check your date with 15th Day of month Like example bellow
if($today < $hDay){
    echo 'Start : '. date("t-m-Y");
}else if ($today >= $hDay){
    echo 'Start :'. $hDay;
}

Here, 1. $today will the current date, 2. $hDay will be the 15th day of the month like 15 Nov

Here is the sample code

echo 'First day of month '.
$fDay = date('01-m-Y');
echo '<br> 15th day of month '.
$hDay = date('d-m-Y', (strtotime($fDay)+ (86400 * 15)));
echo '<br> Last Day of month '.
$lDay = date("t-m-Y");

echo '<br> Current day '.
$today = date('d-m-Y');
//$today =  date('d-m-Y', strtotime($hDay)+86400 );
echo '<br>'; 
if($today < $hDay){
    echo 'Start : '. $lDay = date("t-m-Y");
}else if ($today >= $hDay){
    echo 'Start :'. $hDay;
}
Parag Chaure
  • 2,883
  • 2
  • 19
  • 27
  • it's only displaying Nov 30...how can i code it if i want the output to become nov 30, dec 15, dec 31 and so on... – zuma Nov 09 '15 at 08:00
  • yes, it is only for current month, you can add it on a loop so you can get your output desire output – Parag Chaure Nov 09 '15 at 09:08
1

You can get total number of days in month by

<?php
   $a_date = "2015-11-09";
   echo totaldays = date("t", strtotime($a_date));
?>

You can get current day from current date :

<?php
   echo curday = date('d');
   if(curday==totaldays){
     echo "lastday";
   }
   if(curday==15)
   {
     echo "15th day";
   }
?>
Lalji Nakum
  • 380
  • 1
  • 14
1

find 15th: strtotime($month_sched." +14 day");

find last day of month: strtotime($month_sched." next month - 1 hour");

Please try the following code:

<?php
$y=1;
$num_term = 10;
//start date
$month_sched = date("2012-02-01");
while($y <= $num_term) {
    //15th
    $month_line_15 = strtotime($month_sched." +14 day");
    //last day of month
    $month_line_last = strtotime($month_sched." next month - 1 hour");
    $day = date("M-d", $month_line_15);
    $month_int = date("M-d", $month_line_last);
    $month_sched = date("Y-m-d",strtotime($month_sched." +1month"));
    $y++;
}

Tested.

PHPFiddle

Peter Kota
  • 8,048
  • 5
  • 25
  • 51
  • $month_sched is not fixed...$month_sched would be the current date so for example today is Nov 9 so the first output will be Nov 23 so the output cant have a 15 - 30 sequence..hehe – zuma Nov 09 '15 at 08:29
1

You can use mktime with 0 and 15 as value for the day:

$a = date("m-d", mktime(0,0,0,$month,0));
$b = date("m-d", mktime(0,0,0,$month,15));

This will give you the last day of the previous month and the 15. of $month

svrnm
  • 1,036
  • 6
  • 17
  • For more details, you should read the documentation about mktime: https://secure.php.net/manual/en/function.mktime.php – svrnm Nov 09 '15 at 07:55
0

More than enough answers possible for this one. I would like to contribute an answer as well.

$beginDate = new DateTime('15 January');

// clone start date
$endDate = clone $beginDate;

// Add 1 year to start date
$endDate->modify('+1 year');

// Increase with an interval of one month
$dateInterval = new DateInterval('P1M');

$dateRange = new DatePeriod($beginDate, $dateInterval, $endDate);

foreach ($dateRange as $day) {
    echo $day->format('Y-m-d')."<br />"; // 15th
    echo $day->format('Y-m-t')."<br />"; // Last month day
}

References

PHP Manual - DateTime

PHP Manual - DateInterval

PHP Manual - DatePeriod

PHP Manual - clone

Peter
  • 8,776
  • 6
  • 62
  • 95