1

Hello guys I'm building with my calendar.

I'm trying to get the pre and post day of the current month for my calendar. Actually the calendar is already working but its not the same with the time with my laptop.

Do I have to change something in this code?

$month = 9;
$year  = 2013;

// calculate the number of days in the current month 
$day_count = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// get number of days for the next month 
$pre_days = (6 - (date('w', mktime(0, 0, 0, $month, $day_count, $year))));

// get number of days after the month 
$post_days = date('w', mktime(0, 0, 0, $month, 1, $year));

Then I'm making a loop to display the dates..

echo "<div id='cal_wrapper'>";
echo    "<div class='title_bar'>";
echo        "<div class='prev_month'></div>";
echo        "<div class='show_month'></div>";
echo        "<div class='next_month'></div>";
echo    "</div>"; // end title bar

echo    "<div class='week_days'>";
        foreach($weekDaysArr as $value)
        {
echo        "<div class='days_of_week'>".$value."</div>";

        }
echo   "<div class='clear'></div>";
echo    "</div>"; // end week days

        // Previous Month Filler
        if($pre_days != 0) {
            for($i = 1; $i <= $pre_days; $i++)
            {
echo            "<div class='non_cal_days'></div>";
            }
        }

        // Current day of the month filler
        if($day_count != 0 )
        {
            for($x = 1; $x <= $day_count; $x++)
            {
echo           "<div class='cal_days'>";
echo                "<div class='day_header'>".$x."</div>";
echo            "</div>";
            }
        }

        // Current day of the month filler
        if($post_days != 0 )
        {
            for($y = 1; $y <= $post_days; $y++)
            {
echo            "<div class='non_cal_days'></div>";
            }
        }

echo "</div>"; // end calendar wrapper
Marty McVry
  • 2,838
  • 1
  • 17
  • 23

1 Answers1

0

add the timezone to your script:

<?php
    date_default_timezone_set('America/Los_Angeles');
    // or         date_default_timezone_set('Europe/Berlin');
    .
    .
    .

have a look here for the timezones: http://php.net/manual/en/timezones.php

EDIT

<?php
    $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
    echo $date->format('Y-m-d H:i:sP') . "<br />";

    $date = new DateTime();
    echo $date->format('U = Y-m-d H:i:s') .' <-- right now'; // this should return right now.
?>

for more information http://php.net/manual/en/datetime.settimezone.php

For me it works like this

<?php
    $now = date ( 'Y-m-j G:i:s' );
    echo 'pre_days--> '. $pre_days = date ( 'Y-m-j G:i:s', strtotime ( '-6 day' . $now ) );
    echo '<br />'; 
    echo 'post_days--> '. $post_days = date ( 'Y-m-j G:i:s', strtotime ( '+1 year' . $now ) );
?>
caramba
  • 21,963
  • 19
  • 86
  • 127
  • sir i already set the timezone but still i don't get the exact date. – user2184757 Sep 07 '13 at 16:07
  • sir the date was exact now...thanks..but how can i use that to get the pre and post day..sorry for this kind of question.. – user2184757 Sep 07 '13 at 16:46
  • I'm not sure if $month = 9; $year = 2013; should always stay like that or change. you can also use those vars in strtotime – caramba Sep 07 '13 at 17:38
  • sir, what day do you get? (can u delete some of your comments, you havent got enough points to move to a chat..) – caramba Sep 07 '13 at 18:32
  • $now = date ( 'Y-m-j G:i:s' ); $days = '-6 day'; echo 'sixDaysBeforeNow --> '. $sixDaysBeforeNow = date ( 'Y-m-j G:i:s', strtotime ( $days . $now ) ); – caramba Sep 08 '13 at 13:02