Calendar creator

mail@pastecode.io avatar
unknown
php
2 years ago
2.5 kB
4
Indexable
Never
<?php

// DB Connection

$host = 'localhost';
$username = 'root';
$password = '';
$database = 'calendar';

try {
    $db = new PDO("mysql:name=$host;dbname=$database", $username, $password);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
    die();
}

// Functions

function getQuarter($m) {
    if($m > 0 && $m < 4) {
        return '1';
    } else if($m > 3 && $m < 7) {
        return '2';
    } if($m > 6 && $m < 10) {
        return '3';
    } else {
        return '4';
    }
}

function getKindOfDay($d) {
    if($d == 6) {
        return 'SATURDAY';
    } else if($d == 7) {
        return 'SUNDAY';
    } else {
        return 'BANKDAY';
    }
}

// Generating a calendar

/**
 * 
 */

$start = '2022';
$end = '2031';
$yearStart = date_create($year . '-01-01');
$yearEnd = date_create($end . '-01-01');

$date = date('Y-m-d H:i:s', strtotime($yearStart->format('Y-m-d')));
$endDate = date('Y-m-d H:i:s', strtotime($yearEnd->format('Y-m-d')));
$day = strtotime($date);

$dayOfYear = date('z', $day);
$dayOfYear = (int)$dayOfYear + 1;

$query = "INSERT INTO calendar (Date, Year, Quarter, Month, Week, Day, DayOfYear, Weekday, Fiscal_Year, Fiscal_Quarter, Fiscal_Month, KindOfDay) VALUES (?, ?, ?, ?, ?, ? ,?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->execute(array(
    $date,
    date('Y', $day),
    getQuarter(date('n', $day)),
    date('n', $day),
    date('W', $day),
    date('j', $day),
    $dayOfYear,
    date('N', $day),
    date('Y', $day),
    getQuarter(date('n', $day)),
    date('n', $day),
    getKindOfDay(date('N', $day))
));

$date = date('Y-m-d H:i:s', strtotime('+1 day', strtotime($yearStart->format('Y-m-d'))));


for($i = 1; $i <= 100000000; $i++) {
    $day = strtotime($date);

    if($date == $endDate) {
        break;
    }

    $dayOfYear = date('z', $day);
    $dayOfYear = (int)$dayOfYear + 1;

    $query = "INSERT INTO calendar (Date, Year, Quarter, Month, Week, Day, DayOfYear, Weekday, Fiscal_Year, Fiscal_Quarter, Fiscal_Month, KindOfDay) VALUES (?, ?, ?, ?, ?, ? ,?, ?, ?, ?, ?, ?)";
    $stmt = $db->prepare($query);
    $stmt->execute(array(
        $date,
        date('Y', $day),
        getQuarter(date('n', $day)),
        date('n', $day),
        date('W', $day),
        date('j', $day),
        $dayOfYear,
        date('N', $day),
        date('Y', $day),
        getQuarter(date('n', $day)),
        date('n', $day),
        getKindOfDay(date('N', $day))
    ));

    
    $date = date('Y-m-d H:i:s', strtotime('+1 day', $day));
}
?>