Jump to content

PHP Date and time changing


pmarar

Recommended Posts

Hi

 

We are trying to use one php based helpdesk tool in that I need one help that is SLA time settings default the tool it is 24/7 as per our requirement it is 5 days and timings are like this

 

9AM to 5.30 PM also need to stop sla in defined holidays also, shall it is possible or not ?

The files using are

incidents_escalate.php --> setting escalation time of incident

time.php ---> All time settings for the application

incidents_escalate.php

 

if( $sla_level_id == '' ) {

return false;

}

 

$temp_db->query( 'SELECT s.name AS name,

s.id AS sla_id,

e.time AS time,

e.id AS esc_id,

e.escalate AS escalate,

e.time AS solve_time,

e.escalate_owner AS escalate_owner,

e.escalate_group AS escalate_group,

e.escalate_priority AS escalate_priority,

e.escalate_status AS escalate_status

FROM sla_levels as s,

escalations as e

WHERE e.sla_level_id = s.id

AND s.id IN ( ' . $sla_level_id . ')

AND e.priority_id=' . $incident_row['prio_id'] . '

HAVING e.time = min(e.time)' );

 

$row = $temp_db->fetch_array();

$hours_offset = $row['time'];

 

// Incident does not escalate

if( $hours_offset == '' ) {

return false;

}

 

// Now calc the deadlines

$epoch_deadline = $epoch_incident + ( int )( $hours_offset / 24 );

$secs_deadline = $secs_incident + ( ( $hours_offset % 24 ) * 3600 );

 

// Fill in some values in the array that are from the incident_row

$row['date_reported'] = $incident_row['date_reported'];

$row['name'] = $incident_row['name'];

$row['owner'] = $incident_row['owner'];

 

// Event can't escalate normally, we CAN force escalation tough

if( ( returned_database_boolean( $row['escalate'] ) == false ) &&

( $force_escalation == false ) )

{

return false;

}

 

$epoch_now = dmy_to_days_from_epoch( date( 'd' ), date( 'm' ), date( 'Y' ) );

$secs_now = ( date( 'H' ) * 3600 ) + ( date( 'i' ) * 60 ) + date( 's' );

 

$ta = db_time_to_array( $row['date_reported'] );

$epoch_incident = dmy_to_days_from_epoch( $ta[0], $ta[1], $ta[2] );

$secs_incident = ( $ta[3] * 3600 ) + ( $ta[4] * 60 ) + $ta[5];

 

if( ( ( ( $epoch_deadline - $epoch_now ) * 86400 ) + ( $secs_deadline - $secs_now ) < 0 ) ||

( $force_escalation == true ) )

{

// We're escalating

$log = '';

 

// Update database and create log

if( $row['escalate_owner'] != '' )

{

$temp_db->query( 'UPDATE incidents SET owner_id=' . $row['escalate_owner'] . ' WHERE id=' . $incident_id );

 

$temp_db->query( 'SELECT firstname, middlename, surname FROM users WHERE id=' . $row['escalate_owner'] );

$temprow = $temp_db->fetch_array();

$log .= text( 'owner' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['firstname'] . ' ' . $temprow['middlename'] . ' ' . $temprow['surname'] . "\n";

}

 

if( $row['escalate_group'] != '' )

{

$temp_db->query( 'UPDATE incidents SET group_id=' . $row['escalate_group'] . ' WHERE id=' . $incident_id );

 

$temp_db->query( 'SELECT name FROM groups WHERE id=' . $row['escalate_group'] );

$temprow = $temp_db->fetch_array();

$log .= text( 'group' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['name'] . "\n";

}

 

if( $row['escalate_priority'] != '' )

{

$temp_db->query( 'UPDATE incidents SET priorities_id=' . $row['escalate_priority'] . ' WHERE id=' . $incident_id );

 

$temp_db->query( 'SELECT name FROM priorities WHERE id=' . $row['escalate_priority'] );

$temprow = $temp_db->fetch_array();

$log .= text( 'priority' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['name'] . "\n";

}

 

if( $row['escalate_status'] != '' )

{

$temp_db->query( 'UPDATE incidents SET status_id=' . $row['escalate_status'] . ' WHERE id=' . $incident_id );

 

$temp_db->query( 'SELECT name FROM status WHERE id=' . $row['escalate_status'] );

$temprow = $temp_db->fetch_array();

$log .= text( 'status' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['name'] . "\n";

}

 

if( $log == '' )

{

$log = text( 'no_changes' );

}

 

// There is a reason, alter the text

if( $reason > -1 )

{

if( $reason == 0 )

{

$log = text( 'no_reason' ) . '<br />' . $log;

}

else

{

$temp_db->query( 'SELECT name FROM escalation_reasons WHERE id=' . $reason );

$temprow = $temp_db->fetch_array();

$log = text( 'reason' ) . ': ' . $temprow['name'] . '<br />' . $log;

}

}

 

$log = '<strong>' . text( 'incident_escalation_from' ) . ' ' . $row['name'] . "</strong>\n" . $log;

 

// Add this to incident log

// Note: incident's old owner always escalates an incident

$temp_db->query( 'INSERT INTO incidents_log( incident_id, user_id, changed, log, sla_time )

values( ' . $incident_id . ',

' . $row['owner'] . ',

now(),

\'' . stripslashes( $log ) . '\',

'.$hours_offset.' );' );

 

// Update incident and set the date_escalated to now

$temp_db->query( 'UPDATE incidents SET date_escalated=now() WHERE id=' . $incident_id );

 

 

 

// Email admin about it

if( O_INC_SENDEMAIL )

{

$GLOBALS['email']->email_user( $row['owner'],

'incident_escalated_owner',

array( 'id' => $incident_id ),

true );

}

 

if( O_INC_ESCALATION_SENDADMINEMAIL ) {

$GLOBALS['email']->email_message( ADMIN_EMAIL,

'incident_escalated_admin',

array( 'id' => $incident_id ) );

}

 

return true;

}

 

return false;

}

?>

 

time.php

 

<?php

/ Defines for easy handling of time

 

Please help me to resolve this issue

 

Thanks & Regards

PRASANTH MARRAR

define( 'TIME_YEAR', 2 );

define( 'TIME_MONTH', 1 );

define( 'TIME_DAY', 0 );

define( 'TIME_HOUR', 3 );

define( 'TIME_MINUTE', 4 );

define( 'TIME_SECOND', 5 );

 

 

/**

* Return the current microtime

*/

function stopwatch_start()

{

list( $usec, $sec ) = explode( " ", microtime() );

return ( ( float )$usec + ( float )$sec );

}

 

/**

* Return the time spent since the input time

*/

function stopwatch_stop( $starttime )

{

list( $usec, $sec ) = explode( " ", microtime() );

return ( ( ( float )$usec + ( float )$sec ) - $starttime );

}

 

/*

* Db time to dfe

*/

function db_time_to_dfe( $db_time )

{

return dmy_array_to_days_from_epoch( db_time_to_array( $db_time ) );

}

 

/**

* Calculates d m y to days from epoch

*/

function dmy_array_to_days_from_epoch( $array )

{

return dmy_to_days_from_epoch( $array[0], $array[1], $array[2] );

}

 

/**

* calculates how long since the computer epoch we are

*/

function dmy_to_days_from_epoch( $day, $month, $year )

{

if( ($timestamp=@gmmktime (1, 0, 0, $month, $day, $year)) === -1 ) {

__FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Invalid timestamp( " D:'.$day.', M:'.$month.', Y:'.$year.' ) -> '.$timestamp.'" )' );

return 0;

}

 

 

return ($timestamp / 86400);

}

 

 

/**

* Epoch to array

*/

function epoch_to_dmy( $epoch )

{

// We use day-epcohs

$epoch = $epoch * 86400;

return( array( gmdate('d', $epoch), gmdate('m', $epoch), gmdate('Y', $epoch), gmdate('H', $epoch), gmdate('i', $epoch), gmdate('s', $epoch) ) );

}

 

 

/**

* Calculates EXACT dmy

*/

function exact_epoch_to_dmy( $epoch )

{

return( array( gmdate('d', $epoch), gmdate('m', $epoch), gmdate('Y', $epoch), gmdate('H', $epoch), gmdate('i', $epoch), gmdate('s', $epoch) ) );

}

 

/**

* Calculates days to months and years

*/

function day_to_dmy_array( $days )

{

global $month_days;

$months = $month_days;

 

$year = 1970;

$month = 1;

 

// Calc howmany years we have

while ( true )

{

// Count jaar (leapyear is counted later)

$days -= 365;

$year++;

// Shrikkel

if ( ( $year % 4 == 0 ) && ( $year % 100 != 0 ) )

{

if ( $days < 366 )

{

// echo "+ Break on leap ($year)<br />";

break;

}

$days--;

// Normal

}

else

{

if ( $days < 365 )

{

// echo "+ Normal break<br />";

break;

}

}

}

// echo "v- After yearcount we have $days days left.<br />";

// Calc months

while( $days > $months[$month] )

{

$days -= $months[$month];

$month++;

}

 

if( $days == 0 ) $days = 1;

// echo "v- After monthcount we have $days days left.<br />";

if( $days < 10 ) $days = '0' . $days;

if( $month < 10 ) $month = '0' . $month;

// echo "Days: $days Month: $month Year: $year<br />";

return array( $days, $month, $year, '00', '00', '00' );

}

 

/*

* Integer to time

*/

function int_to_time( $time )

{

$days = '';

$mins = $time % 60;

$hours = ( int )( $time / 60 );

 

if( $mins < 10 )

{

$mins = '0' . $mins;

}

 

$timestring = $hours . ':' . $mins;

 

return $timestring;

}

 

 

/**

* Seconds to time string

*/

function seconds_to_time( $time )

{

$days = '';

$timestring = '';

 

$seconds = $time % 60;

$mins = ( ( int )($time/60) ) % 60;

$hours = ( int )( ($time/60) / 60 );

 

if( $mins < 10 )

{

$mins = '0' . $mins;

}

 

if( $hours > 24 ) {

$timestring .= ((int) ($hours/24) ) .' '.text('days').' ';

$hours = $hours % 24;

}

 

 

$timestring .= $hours . ':' . $mins .':'.$seconds;

 

return $timestring;

}

 

 

/**

* returns now

*/

function now()

{

return array_to_db_time( array( date( 'd' ), date( 'm' ), date( 'Y' ), date( 'H' ), date( 'i' ), date( 's' ) ) );

}

 

/**

* Find out in what week this date is

*/

function date_to_weeknr( $date )

{

if( ($timestamp=strtotime( $date ) ) === -1 ) {

 

__ERROR__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Invalid timestamp: strtotime("'.$date.'") => "'.$timestamp.'"' );

}

 

return date( 'W', $timestamp );

}

 

/**

* Find out on what day a week begins

* Note: we can't do leap years so we just guess if the user does not know if it's a leap year

*/

function weeknr_to_date( $weeknr, $year)

{

global $month_days;

$months = $month_days;

 

if( $weeknr > 53 ) __FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'weeknr_to_date(): week cannot be larger then 53.' );

 

 

// There is no week 53, it's week one of the next year

if( $weeknr == 53 ) {

return array_to_db_date( array( '01', '01', ($year+1) ) );

}

 

// No week zero and start counting at one

$weeknr_days = (($weeknr-1) * 7)+1;

 

// The second problem is finding out what day this year started. As not all years start at monday but all weeks DO !

$offset_day = date( 'w', mktime (0,0,0,1,1,$year) ) - 1; // Minus one because php starts at sunday and the world© at monday

//echo $offset_day.'<br />';

 

// Now fix the weernr days to start not at monday but at the actual day, this is probmeatic as we can go back in time

if( $offset_day >= $weeknr_days ) {

 

// Goes back a year

//echo "   weeknr_to_date($weeknr, $year): PREV YEAR EXECPT array_to_db_date( array( ( 31 - $offset_day ), '12', ($year-1) ) ): ".array_to_db_date( array( ( 31 - $offset_day ), '12', ($year-1) ) )."<br /><br />";

 

return array_to_db_date( array( ( 32 - $offset_day ), '12', ($year-1) ) ); // Dec has 32 days to compensate for 0-1

}

 

// Remains in this year

else {

$weeknr_days -= $offset_day;

}

 

//echo "   weeknr_to_date($weeknr, $year): weeknr_days: $weeknr_days<br />";

 

// Schrikkeljaar

if ( $year % 4 == 0 )

{

$months[02] = 29;

}

 

$month = 1;

while( $weeknr_days >= $month_days[$month] ) {

//echo "   weeknr_to_date($weeknr, $year): while( $weeknr_days >= ".$month_days[$month]." )<br />";

 

$weeknr_days -= $month_days[$month];

$month++;

}

 

 

//echo "   weeknr_to_date($weeknr, $year): return array_to_db_date( array( $weeknr_days, $month, $year ) ): ".array_to_db_date( array( $weeknr_days, $month, $year ) )."<br /><br />";

if( $weeknr_days < 10 ) $weeknr_days = '0' . (int)$weeknr_days;

if( $month < 10 ) $month = '0' . (int)$month;

 

return array_to_db_date( array( $weeknr_days, $month, $year ) );

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/100449-php-date-and-time-changing/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.