nhance/app/Helpers/utility_helper.php

137 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, $filepath)
{
if ($fileToUpload !== null && $fileToUpload->isValid() && !$fileToUpload->hasMoved()) {
$fileName = $fileToUpload->getName();
$fileToUpload->move($filepath, $fileName);
return $fileName;
} else {
return "";
}
}
}
if (!function_exists('file_unlink')) {
function file_unlink($filepath)
{
if (is_file($filepath) && file_exists($filepath)) {
unlink($filepath);
}
}
}
if (!function_exists('compressImage')) {
function compressImage($file, $destinationPath, $newWidth = 100, $newHeight = 100)
{
// Load the image manipulation library
$image = \Config\Services::image();
// Resize and compress the image
$image->withFile($file)
->fit($newWidth, $newHeight, 'center')
->save($destinationPath);
return true;
}
}
if (!function_exists('fancy_date_time_format'))
{
function fancy_date_time_format($datetime,$return_type = 'fancy') {
date_default_timezone_set('Asia/Kolkata');
$currentDateTime = new DateTime();
$passedDateTime = new DateTime($datetime);
// Calculate the interval between the current time and the passed datetime
$interval = $currentDateTime->diff($passedDateTime);
// If the interval is more than 1 month or the year is different, 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;
}
}