Log in

View Full Version : strtotime, too future date?



city_coder
09-08-2008, 01:42 PM
hi guys, just wondering if this is me or not.



<?
$to_date = '23/09/2008';
$to = strtotime($to_date);
echo $to;
?>


if i try and convert a string to time that has a date greater than 5 days in the future then it doesnt seem to work.
if anybody can try it, they'll see, or they should see. unless im doing something wrong???

Strangeplant
09-08-2008, 02:14 PM
Here is a little script that I have tucked away but unused - got it from here: http://www.phpclasses.org/browse/file/23412.html&no_cj_c=0
The class is:
<?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:
<?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>";

?>

city_coder
09-08-2008, 02:23 PM
wow dude that lost me, i know a bit of php but that just blew me away. any chance you could talk it through?

cheers

Strangeplant
09-08-2008, 04:52 PM
A simple method taken from the PHP: date man page is:
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
But, it doesn't take in daylight savings time, and not good when spanning time zones.

HTH

Twey
09-08-2008, 08:15 PM
Your problem is that PHP's date handling isn't very smart, and it seems to default to the ridiculous American MM/DD/YYYY format, possibly the most illogical date format in the world, and doesn't fall back properly onto the date format used in half the rest of the world. If you use an ISO international YYYY-MM-DD, all is fine.

Don't be scared by the class, my initial reaction was 'OVERKILL!' and then I realised that 4/5 of the code was a comment. It's not necessary in order to solve your problem, though.

kuau
09-10-2008, 12:55 PM
Dear city_coder:

I'm not 100% sure I understand what you are trying to do, but if you want to display the final date as dd/mm/yyyy, then input the $to_date using dashes instead of slashes and then convert it like so:


<?php
$to_date = '30-11-2008';
$to = date('d/m/Y', strtotime($to_date));
echo $to;
?>

Works just fine for me no matter what the date. Would this work for your application?
e :)

Twey
09-10-2008, 03:01 PM
Good job, kuau: that does indeed seem to work too :)

Probably ISO is best for internal use in applications, though.

kuau
09-10-2008, 03:22 PM
Thanks, Twey :)