First Commit
This commit is contained in:
Executable
+132
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as DB;
|
||||
|
||||
class DateFormatter
|
||||
{
|
||||
private static $defaultFormat = 'd/m/Y';
|
||||
private static $cachedFormat = null;
|
||||
|
||||
/**
|
||||
* Format a date according to the company's configuration.
|
||||
*
|
||||
* @param string|null $date The date string (Y-m-d or Y-m-d H:i:s).
|
||||
* @param bool $includeTime Whether to include time in the formatted string.
|
||||
* @return string
|
||||
*/
|
||||
public static function format(?string $date, bool $includeTime = false): string
|
||||
{
|
||||
if (empty($date)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Try to detect if date is already formatted (simple check for '/')
|
||||
if (strpos($date, '/') !== false) {
|
||||
return $date;
|
||||
}
|
||||
|
||||
try {
|
||||
$format = self::getCompanyDateFormat();
|
||||
|
||||
// If the format is just date but time is requested, append time format
|
||||
if ($includeTime) {
|
||||
// Heuristic: if format doesn't seem to have time, add it
|
||||
if (strpos($format, 'H') === false) {
|
||||
$format .= ' H:i';
|
||||
}
|
||||
}
|
||||
|
||||
$timestamp = strtotime($date);
|
||||
if ($timestamp === false) {
|
||||
return $date; // Return original if parsing fails
|
||||
}
|
||||
|
||||
return date($format, $timestamp);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Fallback
|
||||
return date(self::$defaultFormat . ($includeTime ? ' H:i' : ''), strtotime($date));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date format for the current company.
|
||||
* Caches the result in memory (static property) and session.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function getCompanyDateFormat(): string
|
||||
{
|
||||
if (self::$cachedFormat) {
|
||||
return self::$cachedFormat;
|
||||
}
|
||||
|
||||
// Check session
|
||||
if (isset($_SESSION['bus_date_format'])) {
|
||||
self::$cachedFormat = $_SESSION['bus_date_format'];
|
||||
return self::$cachedFormat;
|
||||
}
|
||||
|
||||
// Fetch from database if user is logged in
|
||||
if (isset($_SESSION['user_bus_id'])) {
|
||||
try {
|
||||
$busId = $_SESSION['user_bus_id'];
|
||||
$company = DB::table('bus')
|
||||
->where('bus_id', $busId)
|
||||
->select('bus_date_format')
|
||||
->first();
|
||||
|
||||
if ($company && !empty($company->bus_date_format)) {
|
||||
$_SESSION['bus_date_format'] = $company->bus_date_format;
|
||||
self::$cachedFormat = $company->bus_date_format;
|
||||
return self::$cachedFormat;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Log error if needed, but return default for now
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback default
|
||||
self::$cachedFormat = self::$defaultFormat;
|
||||
return self::$cachedFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a formatted date back to MySQL format (Y-m-d).
|
||||
*
|
||||
* @param string $date Formatted date string
|
||||
* @return string|null Y-m-d formatted date or null on failure
|
||||
*/
|
||||
public static function toMySQL(string $date): ?string
|
||||
{
|
||||
if (empty($date)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// If already Y-m-d (contains - and no /)
|
||||
if (strpos($date, '-') !== false && strpos($date, '/') === false) {
|
||||
return $date;
|
||||
}
|
||||
|
||||
// Try to parse from company format
|
||||
$format = self::getCompanyDateFormat();
|
||||
$d = \DateTime::createFromFormat($format, $date);
|
||||
|
||||
if ($d && $d->format($format) === $date) {
|
||||
return $d->format('Y-m-d');
|
||||
}
|
||||
|
||||
// Try common fallback formats
|
||||
$formats = ['d/m/Y', 'm/d/Y', 'Y/m/d'];
|
||||
foreach ($formats as $fmt) {
|
||||
$d = \DateTime::createFromFormat($fmt, $date);
|
||||
if ($d && $d->format($fmt) === $date) {
|
||||
return $d->format('Y-m-d');
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user