ria/app/Helpers/datetime_helper.php
2025-02-05 11:59:39 +00:00

295 lines
8.6 KiB
PHP
Executable File

<?php
if (!function_exists('get_current_date_time')) {
function get_current_date_time()
{
$datetime = new \DateTime('now');
return $datetime->format('Y-m-d H:i:s');
}
}
// if (!function_exists('get_current_date')) {
// function get_current_date()
// {
// return date("Y-m-d");
// }
// }
if (!function_exists('get_current_date')) {
function get_current_date($format = null)
{
$format = $format ?: "Y-m-d";
return date($format);
}
}
if (!function_exists('get_date_time_format')) {
function get_date_time_format($val)
{
$date = new \DateTime($val, timezone: new \DateTimeZone('Asia/Kolkata'));
return $date->format('Y-m-d H:i:s');
}
}
if (!function_exists('get_date_time_dynamical_format')) {
function get_date_time_dynamical_format($inputformat, $outputformat, $val)
{
$format = !empty($outputformat) ? $outputformat : 'Y-m-d H:i:s';
$date = \DateTime::createFromFormat($inputformat, $val, new \DateTimeZone('Asia/Kolkata'));
if (!$date) {
return "Invalid Date: $val (Expected format: $inputformat)";
}
return $date->format($format);
}
}
if (!function_exists('get_date_format')) {
function get_date_format($DateValue)
{
$date = \DateTime::createFromFormat('d/m/Y', $DateValue);
if ($date === false) {
return false;
}
$retDate = $date->format('Y-m-d');
return $retDate;
}
}
// if (!function_exists('format_date')) {
// function format_date($dateString)
// {
// return date('Y-m-d', strtotime($dateString));
// }
// }
// if (!function_exists('format_date')) {
// function format_date($dateString, $days = 0)
// {
// $date = strtotime($dateString);
// // Subtract the specified number of days from the current date if $days is provided
// if ($days != 0) {
// $date = strtotime("-$days day", $date);
// }
// return date('Y-m-d', $date);
// }
// }
if (!function_exists('format_date')) {
function format_date($dateString, $days = 0,$format = 'Y-m-d')
{
// Check if the input is already a Unix timestamp
if (is_numeric($dateString) && (string) (int) $dateString === $dateString) {
$date = strtotime($dateString);
} else {
// Use date_create() to create a DateTime object
$date = date_create($dateString);
}
// If $date is false, meaning parsing failed, return null
if (!$date) {
return null;
}
// Subtract the specified number of days from the date if $days is provided
if ($days != 0) {
// If $date is a Unix timestamp, modify it using strtotime()
if (is_numeric($dateString) && (string) (int) $dateString === $dateString) {
$date = strtotime("-$days day", $date);
} else {
// If $date is a DateTime object, modify it using DateTime::modify()
date_modify($date, "-$days day");
}
}
// If $date is a Unix timestamp, format it using date()
if (is_numeric($dateString) && (string) (int) $dateString === $dateString) {
return date($format, $date);
} else {
// If $date is a DateTime object, format it using DateTime::format()
return date_format($date, $format);
}
}
}
if (!function_exists('get_financial_year')) {
function get_financial_year($date = null)
{
// If no date is provided, use the current date
$date = $date ?: date('Y-m-d');
// Extract the year from the date
$year = date('Y', strtotime($date));
// Determine the financial year based on your business logic
// Adjust this logic according to your organization's financial year cycle
if (date('n', strtotime($date)) < 4) {
// If the month is before April, use the previous year
$financialYear = ($year - 1) . '-' . $year;
} else {
// If the month is April or later, use the current year
$financialYear = $year . '-' . ($year + 1);
}
return $financialYear;
}
}
if (!function_exists('get_financial_year_list')) {
function get_financial_year_list($start_year = null, $num_years = 5)
{
// If no start year is provided, use the current year
$start_year = $start_year ?: date('Y');
$financial_years = [];
// Loop to generate financial years
for ($i = 0; $i < $num_years; $i++) {
$current_year = $start_year - $i;
$next_year = $current_year + 1;
// Determine the financial year
$financial_year = $current_year . '-' . $next_year;
// Add the financial year to the list
$financial_years[] = $financial_year;
}
return $financial_years;
}
}
if (!function_exists('get_shorten_financial_year')) {
function get_shorten_financial_year($date = null)
{
$date = $date ?: date('Y-m-d');
$year = date('y', strtotime($date));
if (date('n', strtotime($date)) < 4) {
$shortenFinancialYear = ($year - 1) . '-' . $year;
} else {
$shortenFinancialYear = $year . '-' . ($year + 1);
}
return $shortenFinancialYear;
}
}
if (!function_exists('date_from_mysql')) {
function date_from_mysql($date, $ignore_post_check = false)
{
if ($date !== '0000-00-00') {
if (!$ignore_post_check) {
$dateObj = DateTime::createFromFormat('Y-m-d', $date);
$retDate = $dateObj->format('Y-m-d');
return $retDate;
}
return $date;
}
return '';
}
}
if (!function_exists('add_one_hour_date')) {
function add_one_hour_date($dateString) {
$date = new DateTime($dateString);
$date->add(new DateInterval('PT1H'));
return $date->format('Y-m-d H:i:s');
}
}
if (!function_exists('get_date_days_ago')) {
function get_date_days_ago($date = null, $format = 'Y-m-d', $days = '-2 days') {
if (is_null($date)) {
$date = new \DateTime('now');
} else {
$date = new \DateTime($date);
}
$timestamp = $date->getTimestamp();
$newDate = date($format, strtotime($days, $timestamp));
return $newDate;
}
}
if (!function_exists('get_fiscal_years')) {
function get_fiscal_years($startOn, $endOn ,$pre_year)
{
$fiscalYearStartDate = new DateTime($startOn);
$fiscalYearEndDate = new DateTime($endOn);
$fiscalYears = [];
for ($i = 0; $i < $pre_year; $i++) {
$fiscalYearStartDate->sub(new DateInterval('P1Y'));
$fiscalYearEndDate->sub(new DateInterval('P1Y'));
$startYear = $fiscalYearStartDate->format('Y');
$endYear = $fiscalYearEndDate->format('Y');
$fiscalYears[] = "$startYear-$endYear";
}
return $fiscalYears;
}
}
if (!function_exists('get_pre_financial_years')) {
function get_pre_financial_years($startOn, $endOn ,$pre_year)
{
$fiscalYearStartDate = new DateTime($startOn);
$fiscalYearEndDate = new DateTime($endOn);
$fiscalYears = [];
for ($i = 0; $i < $pre_year; $i++) {
$fiscalYearStartDate->sub(new DateInterval('P1Y'));
$fiscalYearEndDate->sub(new DateInterval('P1Y'));
$startYear = $fiscalYearStartDate->format('Y');
$endYear = $fiscalYearEndDate->format('Y');
$fiscalYears[] = "$startYear-$endYear";
}
return $fiscalYears;
}
}
if (!function_exists('generate_pos_number')) {
function generate_pos_number($last_pono)
{
$currentYear = date("Y");
$finYear = get_financial_year(); // Get it From DateTime Helper
// if(here have to check fin year if there is a change means both are same means ){
// $counter = $counter + 1;
// }else{
// $counter = 1;
// }
$result = explode('/', $last_pono);
$requestedFinYear = $result[1];
$leftPad = $result[0];
$digit = 5;
if($requestedFinYear == $finYear){
$number = ((int)$result[2])+1;
$PoNo = $leftPad . '/' . $finYear . '/' . $number;
}else{
$counter = 1;
$PoNo = $leftPad . '/' . $finYear . '/' . str_pad($counter, $digit, '0', STR_PAD_LEFT);
}
return $PoNo;
}
}