PHP code example of sambakon / chrono

1. Go to this page and download the library: Download sambakon/chrono library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sambakon / chrono example snippets


## Recommended Usage (Static Methods)

All Chrono methods are available statically, which is the recommended way to use the library:


use SamBakon\Chrono\Chrono;

$chrono = new Chrono();
$date = $chrono->getDate('2023-06-15');
$formatted = $chrono->getDateAsString($date, 'Y-m-d');

use SamBakon\Chrono\Chrono;

// Create a date
$date = Chrono::getDate('2023-06-15 14:30:00');

// Add days to a date
$newDate = Chrono::addDaysToDate($date, 5);
echo $newDate->format('Y-m-d H:i:s'); // 2023-06-20 14:30:00

// Calculer la différence en minutes entre deux dates
$date1 = new DateTime('2023-06-15 10:00:00');
$date2 = new DateTime('2023-06-15 14:30:00');
$minutesDiff = ChronoComputer::getMinuteDateDif($date1, $date2);
echo "Différence: $minutesDiff minutes"; // 270 minutes

// Convertir des unités de temps
$hours = ChronoComputer::convertMinutesToHours(90); // 1.5
$minutes = ChronoComputer::convertHoursToMinutes(2); // 120

// Obtenir le temps écoulé depuis une date (format lisible)
$lastSeen = new DateTime('2023-06-10 14:30:00');
echo ChronoComputer::lastSeenHelp($lastSeen); // "3 Days" (if today is 13/06/2023)

// Get the first day of the week for a given date
$date = Chrono::getDate('2023-06-15'); // A Thursday
$monday = Chrono::getFirstDayOfTheWeekFromDate($date);
echo $monday->format('Y-m-d'); // 2023-06-12 (Monday)

// Formater une date
$formattedDate = ChronoCalendar::formatDateDay($date);
echo $formattedDate; // 15/06/2023

// Obtenir le jour de la semaine
$dayOfWeek = ChronoCalendar::getDayOfWeek($date);
echo $dayOfWeek; // "THURSDAY"

// Obtenir le nom du mois à partir de sa position
echo ChronoCalendar::getMonthFromPosition(6); // "JUN"

// Get the interval of today (from midnight to 23:59:59)
$today = Chrono::getIntervalOfToday();
$start = $today['start']->format('Y-m-d H:i:s');
$end = $today['end']->format('Y-m-d H:i:s');
echo "Today from $start to $end";

// Get all dates between two dates
$startDate = '2023-06-01';
$endDate = '2023-06-03';
$dates = ChronoPeriod::getDatesFromRange($startDate, $endDate);
// Returns ['2023-06-01', '2023-06-02', '2023-06-03']

// Ajuster un intervalle de dates
$interval = ChronoPeriod::adjustFilterInterval(
    new DateTime('2023-06-01'),
    new DateTime('2023-06-15')
);

// Convert a timestamp to a DateTime object
$date = Chrono::timeToDate(1686844800);
echo $date->format('Y-m-d'); // 2023-06-15

// Create a DateTime object from a string
$date = Chrono::getDate('2023-06-15');

// Format a date
$formatted = Chrono::getDateAsString($date, 'Y/m/d');
echo $formatted; // 2023/06/15

// Check if a date is valid
$isValid = ChronoCasting::isValidDate('2023-12-31'); // true

// Convert an abbreviated day to its full name
$fullDay = ChronoCasting::parseDay('Mon'); // "Monday"