Jump to content

[SOLVED] Help passing myrow data to a php.class


Recommended Posts

Hello,

 

I found a php class online that I'd like to use but I'm not keen enough to successfully pass the myrow data to the darned class.

 

The code I have working now takes the myrow data generated from a while clause, passes it through a function and spits out the "date posted" in a table cell. I'd like to try something different.. but anyway, this what I have is working:

 

echo "<td align='right'>";
dateposted($myrow['date']);

 

I'd like to pass it to this class which begins:

 

 
<?php
class cktsTime {
public static function timeAgo(
$datefrom,
$dateto = -1
) {
if ( ($datefrom = intval($datefrom)) <= 0)
return('A long time ago.');

if ( ($dateto = intval($dateto)) <= 0)
$dateto = time();
if ( ($difference = ($datefrom - $dateto)) <= 0)
return('NO time AT ALL elapsed so far.');
...and on and on....

 

The class comes with notes to run this:

$timeElapsedUntilNowAsString = cktsTime::timeAgo($datereceived);

echo('this bla bla bla is '.$timeElapsedUntilNowAsString);

 

But everything I've tried to pass the myrow data to the timeAgo function fails.

 

How do I pass this:

dateposted($myrow['date']);

 

to the timeAgo function rather than the dateposted function?

 

I thought it would be as simple as:

timeAgo($myrow['date']);

 

or

cktsTime::timeAgo($myrow['date']);

 

I also tried setting the $datereceived variable equal to the myrow data in several different places, but that hasn't worked either.

 

Just confused. Trying to make this work.

Thank you in advance for your help pointing me in the right direction.

Ryan

Thank you, Russel.

 

with that I'm getting a similar error:

Fatal error: Call to undefined function cktsTime()

 

I also tried:

$class = public static function();

$class->timeAgo($myrow['date']);

$class;

 

and that produces the error:

Parse error: syntax error, unexpected T_PUBLIC

 

I also tried:

$class = class cktsTime();

$class->timeAgo($myrow['date']);

$class;

 

that produces the error:

Parse error: syntax error, unexpected T_CLASS

 

 

 

I also tried setting the $datefrom variable equal to myrow['date']. Seems that would need to be done, but if the function isn't even being called that wouldn't set anyway, right? ... confusing.

 

thanks for your help sorting this out.

There are some notes included in this class script that I had removed previously for easier reading. Now it seems that for this to work, I need to define @Params -- how do I do that?

 

In my database, the "date" data is in "timestamp" format.

 

Here is the beginning of that script again with the notes:

class cktsTime {

/**
* Calculates elapsed time
*
* Uses UNIX timestamp(s) to perform comparisons.
* Works on a basis timefrom/timeto, whereas timeto is NOW, if argument is omitted.
*
* @param int $datefrom REQUIRED time(stamp) FROM which elapsed time has to be calculated
* @param int $dateto OPTIONAL time(stamp) TO which elapsed time has to be calculated, defaults to
* CURRENT TIME (as returned by PHP time())
* @return
*/
public static function /** @var char*/ timeAgo(
/** @var int*/ $datefrom,
/** @var int*/ $dateto = -1
) {
// assume an error if invalid required argument is passed
if ( ($datefrom = intval($datefrom)) <= 0)
return('A long time ago.');
// typecheck optional arg
if ( ($dateto = intval($dateto)) <= 0)
$dateto = time();

 

I don't want to give this up unless this script simply won't work with my data for one reason or another. I'm just trying to understand the next steps -- googling and reading up online, but still not making the connection.

 

How do I set this Param values?

 

Thank you in advance for your help!

 

the @param notes in the class just talk about what parameters go into the constructor function. You seem to be calling to contructor right, your problem is echoing out the data correct? try

 

$class = new cktsTime();
$time = $class->timeAgo($myrow['date']);
echo "$time";

 

from the little bit of that class I saw, it seems like that time_ago function returns the value of how long its been.

Holly crap -- getting close!

 

That bit actually connected to the class and ran through to here:

$dateto = time();

// get arithmetic difference between both times
// protect from "underflow"
if ( ($difference = ($datefrom - $dateto)) <= 0)
return('NO time AT ALL elapsed so far.');

 

"No time at all elapsed" was returned.

 

Does that mean that the data in the database is somehow being set as the "dateto" value, and then compared to the "dateto" (now) value?

 

Thanks, Mike.

nope not at all. time() is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

 

try using strtotime on your date before you pass it in.

 

for example, given the following code

$time = date("Y-m-d h:i:s");//timestamp format
$date = strtotime($time);
$times = time();
echo $time."<br />";
echo $date."<br />";
echo $times;

would output:

2009-09-15 04:03:32

1253001812

1253045012

well, I tried this:

 

$class = new cktsTime();
$time = $class->timeAgo(strtotime($myrow['date']));
echo "$time";

 

 

and this:

$time = $myrow['date'];
$date = strtotime($time);
$class = new cktsTime();
$times = $class->timeAgo($date);
echo "$times";

 

Unfortunately I got the same message -- "No time elapsed.."

 

hmmm... did I use strtotime correctly?

Thanks, Mike.

 

I echoed both $time and $date from the second block of above code and got:

 

2009-09-12 12:16:41

1252783001

 

What do you think at this point. Broken class script and close the topic? Or try something else to troubleshoot this?

 

Thanks,

Ryan

So here's what I'm calling / echoing:

$time = $myrow['date'];
$date = strtotime($time);
$class = new cktsTime();
$times = $class->timeAgo($date);
echo "$times <br>";
echo "$time <br>";
echo "$date <br>";
echo time();

 

and here are the results:

 

NO time AT ALL elapsed so far.

2009-09-12 12:16:41

1252783001

1253048170

 

Here too is the whole script (below). Thank you very much for your help!

<?php
// file: cktsTime.class.php

/**
* Supposed to manage various date/time isues
*
* Serving static method(s) only, thus comes withOUT a Construictor or any members
*
*/
class cktsTime {

/**
* Calculates elapsed time
*
* Uses UNIX timestamp(s) to perform comparisons.
* Works on a basis timefrom/timeto, whereas timeto is NOW, if argument is omitted.
*
* @param int $datefrom REQUIRED time(stamp) FROM which elapsed time has to be calculated
* @param int $dateto OPTIONAL time(stamp) TO which elapsed time has to be calculated, defaults to
* CURRENT TIME (as returned by PHP time())
* @return
*/
public static function /** @var char*/ timeAgo(
/** @var int*/ $datefrom,
/** @var int*/ $dateto = -1
) {
// assume an error if invalid required argument is passed
if ( ($datefrom = intval($datefrom)) <= 0)
return('A long time ago.');
// typecheck optional arg
if ( ($dateto = intval($dateto)) <= 0)
$dateto = time();

// get arithmetic difference between both times
// protect from "underflow"
if ( ($difference = ($datefrom - $dateto)) <= 0)
return('NO time AT ALL elapsed so far.');

// come to here, $difference holds the "raw" value in SECONDS
// default return value with SECONDS,
// this saves ONE check, namely for "< 60"
$c = 's';
// for readability, define some defaults
$min = 60;
$hour = ($min * 60);
$day = ($hour * 24);
$week = ($day * 7);
$month = ($day * 30);
$year = (intval($day * 366.5));

// check for a difference like between one MIN and one HOUR
if ( ($difference >= $min) && ($difference < $hour))
$c = 'n';
// check for a difference like between one HOUR and one DAY
elseif ( ($difference >= $hour) && ($difference < $day))
$c = 'h';
// check for a difference like between one DAY and one WEEK
elseif ( ($difference >= $day) && ($difference < $week))
$c = 'd';
// check for a difference like between one WEEK and one MONTH
elseif ( ($difference >= $week) && ($difference < $month))
$c = 'ww';
// check for a difference like between one MONTH and one YEAR
elseif ( ($difference >= $month) && ($difference < $year))
$c = 'm';
else
$c = 'y';

/*
* Based on the interval, determine the
* number of units between the two dates
* From this point on, you would be hard
* pushed telling the difference between
* this function and DateDiff. If the $datediff
* returned is 1, be sure to return the singular
* of the unit, e.g. 'day' rather 'days'
*/

// default return char
$retchar = '';

// determine interval
switch($c) { // switch
case "m": { // month
$months_difference = floor($difference / 60 / 60 / 24 / 29);
// generate as lon as current time (dateto) hasn't been reached
while (
mktime(
date("H", $datefrom), date("i", $datefrom),
date("s", $datefrom), date("n", $datefrom)+($months_difference),
date("j", $dateto), date("Y", $datefrom)
) < $dateto
)
$months_difference++;
// assign difference
$datediff = $months_difference;
/**
* We need this in here because it is possible
* to have an 'm' interval and a months
* difference of 12 because we are using 29 days
* in a month
*/
if($datediff==12)
$datediff--;
$retchar = 'month';
break;
} // month

case "y": { // year
$datediff = floor($difference / 60 / 60 / 24 / 365);
$retchar = 'year';
break;
} // year
case "d": { // day
$datediff = floor($difference / 60 / 60 / 24);
$retchar = 'day';
break;
} // day
case "ww": { // week
$datediff = floor($difference / 60 / 60 / 24 / 7);
$retchar = 'week';
break;
} // week
case "h": { // hour
$datediff = floor($difference / 60 / 60);
$retchar = 'hour';
break;
} // hour
case "n": { // minute
$datediff = floor($difference / 60);
$retchar = 'minute';
break;
} // minute
case "s": { // second
$datediff = $difference;
$retchar = 'second';
break;
} // second
} // switch

// take care of proper grammar,
// thus month/months, year/years, and so on
if ($datediff != 1)
$retchar .= 's';

// send home...
// creating a "value" is actually overhead
// however, certain environments REQUIRE an ASSIGNMENT prior to return,
// so, for safety's sake...
return( ($c = $datediff.' '.$retchar.' ago'));

} // END timeAgo()

} // END CLASS

// class above is easy to use
// EXAMPLE

/**
* You receive a date (from where ever) as a timestamp
* if you want to know time elapsed until NOW, you simply pass that
* timestap the (static) class method
* NOTE: I certainly assume $datereceived to being retrieved from somewhere...
*/

$timeElapsedUntilNowAsString = cktsTime::timeAgo($datereceived);
echo('this bla bla bla is '.$timeElapsedUntilNowAsString);
// puts out 'this bla bla... is one week ago

/**
* In case you have a second time (prior to NOW), you passed that one, too.
* Again, assuming there is a value (int) $datetocompareto
*
*/
$timeElapsedBetweenDatesAsString = cktsTime::timeAgo($datereceived,$datetocompareto);

/**
* The good things (or ONE of them) with classes is, that you can extend them at any time, NOT
* breaking any code written prior to changes.
*
* Have Fun,
* ariell.
*/
?> 

hmm, well whats happening is that when the function subtracts your input date from the value of time() it returns a negative number. whoever wrote this function didn't do it right I think (well the logic anyways) but try this

$times = $class->timeAgo(time(), $date);

 

and see what happens

Hi Mike,

 

You did it. It's working now. Thank you very much.

 

The only thing that is weird is that when I post a comment, it returns "0 years ago" -- ha!

 

Then if I wait two minutes and refresh the page it retures, "2 minutes ago"

 

do you see a code tweak to fix that?

 

thank you again so much!

Ryan

curious. thats  because of this particular snippet of code from the functin

// check for a difference like between one MIN and one HOUR
if ( ($difference >= $min) && ($difference < $hour))
$c = 'n';
// check for a difference like between one HOUR and one DAY
elseif ( ($difference >= $hour) && ($difference < $day))
$c = 'h';
// check for a difference like between one DAY and one WEEK
elseif ( ($difference >= $day) && ($difference < $week))
$c = 'd';
// check for a difference like between one WEEK and one MONTH
elseif ( ($difference >= $week) && ($difference < $month))
$c = 'ww';
// check for a difference like between one MONTH and one YEAR
elseif ( ($difference >= $month) && ($difference < $year))
$c = 'm';
else
$c = 'y';

it seems that all the checks are running false on the first run, and then running fine on the second. I don't really know why it does that, but I can't think of a quick fix off the top of my head. If it works on the second time around but not the first, i'm not sure why it would do that.

Yep. I screwed around with it for the past 20 minutes or so and -- looks like this part isn't rendering or parsing correctly:

 

case "s": { // second

$datediff = $difference;

$retchar = 'second';

break;

} // second

} // switch

 

would it be $datediff = $difference;, though -- wouldn't it be "less than" 60 but greater than 1 ?

 

I don't know. If you have time to revisit it and offer some suggestions, great.

 

Thank you again for your help getting this to work Mike.

 

Cheers,

Ryan

Found a fix ---

 

After that if statement that kept throwing the "No time at all has elapsed" error I added:

 

if ( ($difference = ($datefrom - $dateto)) <= 60)

return('Less than a minute ago.');

 

That fixed the "0 years ago" problem.

 

Thanks again Mike all for your help. Glad to have this working!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.