Functions to transform dates & times in php

Home Magento TopicsFunctions to transform dates & times in php
php

Functions to transform dates & times in php

No Comments

Functions to transform dates & times in php.

This group of php functions, will help you to transform date and time fields and therefore show the information as you prefer.

We hope this will help you!

/**
 * functionsDateTime.php :  transformation of date & time fields.
 */
 
 
/**
 * Returns name of day of a date informed.
 *
 * @param string $fromData (yyyy-mm-dd)
 * @param array $arrDayName (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday, 7 = Sunday)
 * @return string
 */
function weekdayname ($fromData, $arrDayName)
{
	$intTimeStamp = mktime(0, 0, 0, substr($fromData, 5, 2), substr($fromData, 8, 2), substr($fromData, 0, 4));
	$weekDay = date('w', $intTimeStamp);
    return $arrDayName[$weekDay];
}
 
 
/**
 * Return name of month of a date informed.
 *
 * @param string $fromData (yyyy-mm-dd)
 * @param array $arrMonthName  (0 = January, 1 = February 2 = March 3 = April, 4 = May, 5 = June, 6 = July, 7 = August, 8 = September, 9 = October, 10 = November, 11 = December)
 * @return string
 */
function monthname ($fromData, $arrMonthName)
{
	$intTimeStamp = mktime(0, 0, 0, substr($fromData, 5, 2), substr($fromData, 8, 2), substr($fromData, 0, 4));
	$intMonth = date('m', $intTimeStamp);
    return $arrMonthName[$intMonth - 1];
}
 
/**
 * Transform a date depending format informed.
 *
 * @param string $fromData
 * @param string $strFormat
 * Possible formats are: 'YYYYMMDD_DDMMYYYY','YYYYMMDD_DDMMYY','DDMMYYYY_YYYYMMDD'
 * @return string
 */
function transformdate($fromData, $strFormat ='DDMMYYYY_YYYYMMDD')
{
	$newDate = '';
	switch ($strFormat){
		case 'YYYYMMDD_DDMMYYYY':
			$newDate = substr($fromData, 8, 2).substr($fromData, 4, 4).substr($fromData, 0, 4).substr($fromData, 10);
		break;
		case 'YYYYMMDD_DDMMYY':
			$newDate = substr($fromData, 9, 2).substr($fromData, 6, 2).substr($fromData, 3, 2);
		break;	
		case 'DDMMYYYY_YYYYMMDD':
			$newDate = substr($fromData, 6, 4).substr($fromData, 2, 4).substr($fromData, 0, 2).substr($fromData, 10);
		break;		
	}
    return $newDate;
}
 
 
function transformtime($time){
 
if ($time == '') return '00:00:00';
if ($time == '00:00:00') return '00:00:00';
 
$pattern="/^([0-1][0-9]|[2][0-3])[\:]([0-5][0-9])[\:]([0-5][0-9])$/";
if(preg_match($pattern,$time))
     return $time;
return '00:00:00';
 
}
 
 
/**
 * Return end date of informed date. 
 *
 * @param string $fromData
 * @return string
 */
function lastmonthday($fromData)
{
	$newDate = '';
	$intTimeStamp = mktime(0, 0, 0, substr($fromData, 5, 2), substr($fromData, 8, 2), substr($fromData, 0, 4));
	$numDays = intval(date("t",$intTimeStamp));	
	$newDate = substr($fromData, 0, 4).substr($fromData, 4, 4).$numDays;
	return $newDate;
}
 
 
/**
 * Return starting date of informed day. 
 *
 * @param string $fromData
 * @return string
 */
function startingmonthday($fromData)
{
	$newDate = '';
	$newDate = substr($fromData, 0, 4).substr($fromData, 4, 4).'1';
	return $newDate;
}

Leave a Reply

Your email address will not be published. Required fields are marked *