nhance/app/Helpers/utility_helper.php
2024-02-16 17:08:31 +05:30

119 lines
4.0 KiB
PHP

<?php
// File: app/Helpers/Uuid_helper.php
if (!function_exists('generate_uuid')) {
function generate_uuid($version = 4, $format = 'hex')
{
$data = random_bytes(16);
// echo $data.'<br>';
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
return $uuid;
}
}
if (!function_exists('change_date_format')) {
function change_date_format($data, $source_format, $output_format)
{
// echo $data, $source_format, $output_format;return true;
try {
// Create DateTime object with the source format
$dateTime = DateTime::createFromFormat($source_format, $data);
// Check if the DateTime object is created successfully
if ($dateTime === false) {
throw new Exception('Invalid date or format');
}
// Format the DateTime object with the output format
$formattedDate = $dateTime->format($output_format);
return $formattedDate;
} catch (Exception $e) {
// Handle the exception (e.g., log it, show a user-friendly message)
return "Error: " . $e->getMessage();
return $data;
}
}
}
if (!function_exists('file_Upload')) {
function file_Upload($fileToUpload, $imageDetails = null)
{
$fileName = $fileToUpload->getClientName();
if ($fileToUpload !== NULL && $fileName !== "") {
if ($fileToUpload->isValid() && !$fileToUpload->hasMoved()) {
// Check if the image exists in the upload folder
$existingImagePath = ROOTPATH . 'public/uploads/' . $imageDetails; // Adjust filename field based on your database structure
if ($imageDetails !== null && file_exists($existingImagePath)) {
// If the image exists, delete it
unlink($existingImagePath);
}
// Move the new image to the upload folder
$fileToUpload->move(ROOTPATH . 'public/uploads', $fileName);
}
} else {
$fileName = "";
}
return $fileName;
}
}
if (!function_exists('fancy_date_time_format'))
{
function fancy_date_time_format($datetime,$return_type = 'fancy') {
$currentDateTime = new DateTime();
$passedDateTime = new DateTime($datetime);
// Calculate the interval between the current time and the passed datetime
$interval = $currentDateTime->diff($passedDateTime);
// return change_date_format($datetime,'Y-m-d H:i:s','d M Y h:i a');
// If the interval is more than 1 month, return the original datetime
if ($interval->m > 1 || $interval->y > 0) {
if($return_type = 'fancy'){ return change_date_format($datetime,'Y-m-d H:i:s','d M Y h:i a'); }
return $datetime;
} elseif ($interval->m == 1 && $interval->y == 0) {
return "1 month ago";
} elseif ($interval->d >= 7) {
$weeks = floor($interval->d / 7);
return $weeks == 1 ? "1 week ago" : "$weeks weeks ago";
} elseif ($interval->d >= 1) {
return $interval->d == 1 ? "1 day ago" : $interval->d . " days ago";
} elseif ($interval->h >= 1) {
return $interval->h == 1 ? "1 hour ago" : $interval->h . " hours ago";
} elseif ($interval->i >= 1) {
return $interval->i == 1 ? "1 minute ago" : $interval->i . " minutes ago";
} else {
return "Just now";
}
}
}
if(!function_exists('check_string_date'))
{
function check_string_date($str)
{
if (DateTime::createFromFormat('Y-m-d H:i:s', $str) !== false) {
return true;
}
return false;
}
}