Skip to content

Instantly share code, notes, and snippets.

@faffyman
Created January 31, 2012 09:45
Show Gist options
  • Save faffyman/1709657 to your computer and use it in GitHub Desktop.
Save faffyman/1709657 to your computer and use it in GitHub Desktop.
A quickie just-for-fun script used to count the days until Christmas. Originally used as part of a SOAP service hence the XML output.
<?php
/*
* This class was originally written as part of a SOAP service - just for fun
*
* used as a countdown until Christmas
* Returns
* the number of Days
* the number of Shopping Days
* the number of Weekends
* */
class SoapDaysUntilChristmas {
function __construct($aParams)
{
$dtToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
$dtXmas = mktime(0,0,0,12,25,date('Y'));
if ($dtToday>=$dtXmas) {
$dtXmas = mktime(0,0,0,12,25,date('Y')+1);
}
//Using simple division
$sec2xmas = ($dtXmas - $dtToday) ;
$secperday = (60 * 60 *24) ;
$this->days2xmas = (int) ($sec2xmas / $secperday) ;
$weeks = $this->days2xmas / 7 ;
$this->shoppingDays = (int)($this->days2xmas - $weeks) ;
// By Looping through
$bNonShop = 'sunday' ;
$cAllDays = 0;
$cShopDays = 0 ;
$cWeekends = 0 ;
while ( $dtToday < $dtXmas ) {
//First Count ALL Days
$cAllDays++;
//what day is it
$sDayOfWeek = strtolower(date('l',$dtToday)) ;
//if it's not a sunday then count as a shop day
if ($sDayOfWeek!=$bNonShop) {
$cShopDays++;
}
//if it's a saturday then count as a weekend
if ($sDayOfWeek=='saturday') {
$cWeekends++;
}
//now add on 1 day
$dtToday+=86400 ;
}
$this->summary = '
<NumberOfDaysToChristmas>'.$cAllDays.'</NumberOfDaysToChristmas>
<ShoppingDaysToChristmas>'.$cShopDays.'</ShoppingDaysToChristmas>
<NumberOfWeekendsToChristmas>'.$cWeekends.'</NumberOfWeekendsToChristmas>';
//process the result set into XML
$this->buildXML();
}//end constructor
/**
* Process the result set into an XML format
* @access public
* @return void
* */
function buildXML()
{
$sXML = '<?xml version="1.0" encoding="UTF-8"?>
<DaysToXmas
xmlns="http://xml.mysite.com/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xml.mysite.com/schemas/plain/DaysToXmas.xsd">';
$sXML.= $this->summary ;
$sXML.='
</DaysToXmas>';
$this->SOAP .=$sXML;
//return $sXML;
}//end function
}//end class
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment