[thechat] determining most recent midnight

Paul Ding LancaStir at webtv.net
Fri Feb 21 12:23:00 CST 2003


On 20 Feb 2003 at 19:18, gozz wrote:

> Was going to post this to the list, but solved my problem
> by converting to a string date w/o the time and then
> parsing that back into a unix epoch time ... but ... I can't
> figure out what's wrong with the way I was trying to
> mathematically figure out when the most recent midnight
> was:

> $unix_time = seconds since Jan 1, 1970;
> $seconds_in_day = 60 * 60 * 24;
> $seconds_since_most_recent_midnight = $unix_time %
> $seconds_in_day;
> $most_recent_midnight = $unix_time -
> $seconds_since_most_recent_midnight;

> Where do I go wrong there?

Works just fine.

$unix_time = time();
print "Unix time is " . $unix_time . "\n";
$seconds_in_day = 60 * 60 * 24;
print "Seconds in day = " . $seconds_in_day . "\n";
$seconds_since_most_recent_midnight = $unix_time % $seconds_in_day;
print "Seconds since last midnight = " . $seconds_since_most_recent_midnight . "\n";
$most_recent_midnight = $unix_time - $seconds_since_most_recent_midnight;
print "Most recent midnight = " . $most_recent_midnight . "\n";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
gmtime($most_recent_midnight);

print "Month is $mon\n";
print "Day is $mday\n";
print "Hour is $hour\n";
print "Minute is $min\n";
print "Second is $sec\n";

Unix time is 1045835951
Seconds in day = 86400
Seconds since last midnight = 50351
Most recent midnight = 1045785600
Month is 1
Day is 21
Hour is 0
Minute is 0
Second is 0

(Month is 1 because January is 0.)

deke

--
If I have seen farther than others, it is by stepping on the toes of giants





More information about the thechat mailing list