Currently i'm using a jQuery Calendar Plugin to display a Calendar i've built. All the data is filled in from a SQL database using PHP. I'm at the point with the calendar where I need to start going forwards and backwards in time. I'm having a lot of trouble doing this for week, and month. For just the day view it wasn't bad.
This is my code for the day
function startTime() {
$today = getdate();
$startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);}
I then pass $startTime
into another function which makes a SQL request. $_SESSION[ 'number']
is set to 0 if it hasn't been set before, otherwise I add 1 or subtract 1 depending on if the user hits next or previous. endTime()
is nearly the same function.
I'd like to only have to make changes to my startTime()
and endTime()
functions if possible.
Heres the messy, and untested (Our servers down atm) code for calculating Day/Week/Month
function startTime($type)
{
$today = getDate();
$startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);
if ($type == "day")
{
$startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);
}
if ($type == "week")
{
if ($today['w'] == 0)
{
$startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
}
else
{
$day = strtotime("last Sunday");
$today = getdate($day);
$startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
}
}
if ($type == "month")
{
$startTime = mktime(8, 0, 0, $today["mon"]+($_SESSION['number']), 0, $today['year']);
}
return $startTime;
}
function endTime(type)
{
$today = getdate();
$endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);
if ($type == "day")
{
$endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);
}
if ($type == "week")
{
if ($today['w'] == 6)
{
$endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
}
else
{
$day = strtotime("next Saturday");
$today = getdate($day);
$endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
}
}
if ($type == "month")
{
$day = strtotime("next month - 1 hour");
$today = getdate($day);
$endTime = mktime(8, 0, 0, $today["mon"]+($_SESSION['number']), $today["mday"], $today['year']);
}
return $endTime;
}
I'd like to thank everyone for their time, this is my first post so please let me know if there are any poor practices in my post. I've searched stackoverflow, and google for a solution to this issue and haven't quite found one.
For future reference for code review use https://codereview.stackexchange.com/ if you are having problems getting code to work post it at http://stackoverlow.com. I would use strtotime
instead of mktime
and make one function that returns both start and end time:
function getTimes($type) {
$end_number = $_SESSION['number'] + 1;
$extra = '-1 day';
if ($type == "day") {
$end_number = $_SESSION['number'];
$extra = '';
$date_start = date('Y-m-d');
} elseif ($type == 'week') {
$date_start = date('Y-m-d',strtotime('now -'.date('w').' days'));
} elseif ($type == 'month') {
$date_start = date('Y-m-01');
}
$operator = '+';
if($_SESSION['number'] < 0) $operator = '';
$startTime = strtotime("$date_start 08:00:00 {$operator}{$_SESSION['number']} {$type}s");
$endTime = strtotime("$date_start 20:00:00 {$operator}{$end_number} {$type}s $extra");
return array('startTime'=>$startTime,'endTime'=>$endTime);
}
I guess if you need two different functions you could just add them like this:
function startTime($type) {
$times = getTimes($type);
return $times['startTime'];
}
function endTime($type) {
$times = getTimes($type);
return $times['endTime'];
}