Here is a little script that I have tucked away but unused - got it from here: http://www.phpclasses.org/browse/fil...html&no_cj_c=0
The class is:
Code:
<?php
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Author : Carl Friis-Hansen
Date : June 21st 2008
License: Freeware
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
interface iunixDate {
public function __construct( $utc=false );
// Plain construction of object
public function date( $param="now", $format="%Y/%0m/%0d - %0k:%0M - %Z" );
// Function returnes the result of
// the $param and $format supplied.
// Default: Current date and time
// like 2009/06/23 - 13:20 - CEST
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This class uses the great power of the standard UNIX/Linux/OSX function
called "date", thus this class will only work for web servers hosted on
one of these platforms and assuming this web server has the right to
perform such a task. You can try this to see if it works:
$date = new unixDate;
print " Date and time right now is ".$date->date()." ";
The advantage of using the operating systems standard commands is that
these commands are tested extremely well and are updated in case of change
in the world order, time zones, etc.
Secondly date is very human like to use:
$date = new unixDate;
$result = $date->date( "2005-12-15 +1 year +2 days +5 hours +30 minutes" );
Will put "2006/12/17 - 05:30 - CET" in $result for central Europe.
$date = new unixDate;
$result = $date->date( "now +1 year +2 days +5 hours +30 minutes" );
Will put the current date/time plus 1 year 2 days 5 hours and 30 minutes
in $result.
Below is a list of the output format operators:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 21)
%d day of month (e.g, 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour ( 0..23)
%l hour ( 1..12)
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric timezone (e.g., -0400)
%:z +hh:mm numeric timezone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
For futher reading please use Google around for something like:
+linux +date calculate
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
} // interface iunixDate
class unixDate implements iunixDate {
protected $utc;
public function __construct( $utc=false ) {
$this->utc = $utc;
}
public function date( $param="now", $format="%Y/%0m/%0d - %0k:%0M - %Z" ) {
if( $this->utc ) {
$utcopt = "-u ";
} else {
$utcopt = "";
}
return shell_exec( "date ".$utcopt."-d \"".escapeshellcmd( $param )."\" +\"".escapeshellcmd( $format )."\"" );
} // date(...)
} // class unixDate
?>
And the usage is this:
Code:
<?php
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Simple example using web interface.
Should output something like:
The future: 2009/06/23 - 15:00 - CET
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
require_once "./class.unixDate.php";
$date = new unixDate;
$result1 = $date->date();
$result2 = $date->date( "now +1 year +2 days +5 hours +30 minutes",
"%Y/%0m/%0d - %0k:%0M - %Z" );
print "<html><head><title>Check Date</title></head><body>\n";
print "<h2>Checking class.unixDate.php</h2>\n";
print "------------------------------------------------------<br />\n";
print "The future: ".$result2."<br />\n";
print "is right now plus 1 year 2 days 5 hours and 30 minutes.<br />\n";
print "Right now is by the way ".$result1."<br />\n";
print "------------------------------------------------------<br />\n";
print "2008/12/15 +30 days = ".
$date->date( "2008/12/15 +30 days", "%Y/%0m/%0d" ).
"<br />\n";
print "------------------------------------------------------<br />\n";
print "2009/01/14 -30 days = ".
$date->date( "2009/01/14 -30 days", "%Y/%0m/%0d" ).
"<br />\n";
print "------------------------------------------------------<br />\n";
print "</body></html>";
?>
Bookmarks