Hey,
Thanks for replying.

I did find an easier way, but only after going through what you sent
me...the way i found was using substr.
I do read the manual, but not the online one, I have a downloaded windows
helpfile copy, its much faster
and easier to access but one disadvantage is...it does not have the user
comments.
Cheers,
-Ryan


http://Bestwebhosters.com


----- Original Message ----- 
From: "Justin French" <[EMAIL PROTECTED]>
To: "Ryan A" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Monday, February 02, 2004 1:21 AM
Subject: Re: [PHP] comparing dates


> On Monday, February 2, 2004, at 10:04  AM, Ryan A wrote:
>
> > which gives me two 14 numberic characters strings like:
> > 20040202091212
> > 20040201070500
> >
> > How do I compare it to display something like this to the visitor:
> > You have $xdays day/s, $xhours hours and $xmins minutes before your
> > account
> > expires
> >
> > Been hitting the manual, but have either been searching in the wrong
> > places
> > or....?
> > I think I will have to use "explode","strtotime" on this...but am not
> > sure.
>
> Ryan,
>
> explode() won't help (nothing to explode on)
> strtotime() won't help, as it can't work directly with the above date
> format
>
> There's a decent comment on http://php.net/strtotime (RTFM????) by
> 'tobi at schluer dot de'
> "...You can read the db using the UNIX_TIMESTAMP()-function, which
> returns the timestamp from the date(time) field.  So you don't need to
> convert the dates in PHP, but let SQL do that job."
>
> So, you can do it with MySQL.  Otherwise, a function like this in PHP
> would do it:
>
> <?
> function mysqlToUnixStamp($in)
> {
> $y = substr($in,0,4);
> $m = substr($in,4,2);
> $d = substr($in,6,2);
> $h = substr($in,8,2);
> $i = substr($in,10,2);
> $s = substr($in,12,2);
> return mktime($h,$i,$s,$m,$d,$y);
> }
> ?>
>
> So then pass your mysql stamps to it like this:
>
> <?
> $d1 = '20040202091212';
> $d2 = '20040201070500';
>
> $d1 = mysqlToUnixStamp($d1);
> $d2 = mysqlToUnixStamp($d2);
> ?>
>
> An you can then compare them (for example):
>
> <?
> if($d1 >= $d2) { /*something*/ }
> ?>
>
> Or find the difference in seconds:
>
> <?
> $diff = $d1 - $d2;
> ?>
>
> Converting this into a human readable "$xdays day/s, $xhours hours and
> $xmins minutes" is not exactly easy, but here's some hints for you to
> build on:
>
> <?
> function secondsToString($in)
> {
> $secsInDay = (60 * 60) * 24;
> $secsInHour = 60 * 60;
> $secsInMin = 60;
> $ret = '';
>
> if($in > $secsInDay)
> {
> $days = round($in / $secsInDay);
> $remainder = $in % $secsInDay;
> $ret .= "{$days} days, ";
> }
>
> if($remainder > $secsInHour)
> {
> $hours = round($remainder / $secsInHour);
> $remainder = $in % $secsInHour;
> $ret .= "{$hours} hours, ";
> }
>
> if($remainder > $secsInMin)
> {
> $mins = round($remainder / $secsInMin);
> $remainder = $in % $secsInMin;
> $ret .= "{$mins} mins, ";
> }
>
> // strip off last ', '
> $ret = substr($ret,0,-2);
>
> return $ret;
> }
>
> // converting to human readable
> $timeLeft = secondsToString($diff);
>
> // print to screen
> echo "you have {$timeLeft} remain on your membership";
> ?>
>
>
> Out of all this, what you need to look-up in the manual and understand
> is the modulus (%), an arithmetic operator & substr().
>
> http://www.php.net/manual/en/language.operators.arithmetic.php
> http://www.php.net/substr
>
>
> Justin French
>
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to