 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 8th, 2013, 04:14 AM
|
|
Registered User
|
|
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Undefined offset error agoTimeFormat class
Quote:
Hi Everyone,
i am creating agoTimeFormat class in php but i am getting error in line 16 and 17, here is my code.
|
PHP Code:
<?php
class convertToAgo {
function convert_datetime($str) {
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
function makeAgo($timestamp){
$difference = time() - $timestamp;
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 1; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
}
?>
Quote:
OUTPUT WITH ERROR:
Notice: Undefined offset: 7 in C:\xampp\htdocs\class\agoTimeFormat.php on line 16
Notice: Undefined offset: 7 in C:\xampp\htdocs\class\agoTimeFormat.php on line 17
Warning: Division by zero in C:\xampp\htdocs\class\agoTimeFormat.php on line 17
Notice: Undefined offset: 8 in C:\xampp\htdocs\class\agoTimeFormat.php on line 16
...
...
...
Notice: Undefined offset: 18208 in C:\xampp\htdocs\class\agoTimeFormat.php on line 17
Warning: Division by zero in C:\xampp\htdocs\class\agoTimeFormat.php on line 17
Notice: Undefined offset: 18209 in C:\xampp\htdocs\class\agoTimeFormat.php on line 16
Notice: Undefined offset: 18209 in C:\xampp\htdocs\class\agoTimeFormat.php on line 17
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\class\agoTimeFormat.php on line 17
plz help i m getting 20k error, Thanks
|
|
|

June 11th, 2013, 03:24 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
Fixed:
Code:
<?php
class convertToAgo
{
function convert_datetime($str)
{
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
function makeAgo($timestamp)
{
$difference = time() - $timestamp;
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","1","10");
$text = '';
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1)
{
$periods[$j].= "s";
}
$text = "$difference $periods[$j] ago<br />";
return $text;
}
}
$ago = new convertToAgo();
// Specify date which should be in YYYY-MM-DD HH:MM:SS format
$date = '1972-04-14 17:10:33';
// create the timestamp for the specified date
$ts = $ago->convert_datetime($date);
// do conversion
echo $ago->makeAgo($ts);
?>
|
|

June 11th, 2013, 04:30 AM
|
|
Registered User
|
|
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Still Same error............................
Quote:
-12597 secs ago
its gives in negative value. Why?
|
PHP Code:
-12597 secs ago
its gives in negative value. Why?
|
|

June 11th, 2013, 04:43 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
No it's not the same error.
You're getting a negative value because the date/time is in the future.
For example using:
Code:
2013-06-11 10:10:33
on the 11-06-2013 at 09:38 results in: -1999 secs ago
But using:
Code:
2013-04-11 10:10:33
on the 11-06-2013 at 09:40 results in: 2 months ago
and using:
Code:
2013-06-11 08:10:33
on the 11-06-2013 at 09:41 results in: 2 hours ago
Therefore your class can only handle dates/times in the past if you want it to handle future dates/times you'll need to cater for this.
|
|
The Following User Says Thank You to UseLess For This Useful Post:
|
|
|

June 11th, 2013, 05:12 AM
|
|
Registered User
|
|
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
ok... i understand
PHP Code:
so what is the actual solution.
|
|

June 11th, 2013, 06:45 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
Change the function to test the timestamp to see if it's greater than the current time if it is take the timestamp away from the current time and set a string to be 'ago' <else> take the current time away from the timestamp and set the string to be 'from now' for example.
|
|
The Following User Says Thank You to UseLess For This Useful Post:
|
|
|

June 11th, 2013, 01:06 PM
|
|
Registered User
|
|
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
ok, thanks......
PHP Code:
i think i have used DATETIME datatype instead of using TIMESTAMP type in sql while creating fields in table.
is this so?
|
|

June 12th, 2013, 02:32 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
And we would know you've used the wrong datatype how?
And you alter the function like this;
Code:
function makeAgo($timestamp)
{
$now = time();
if( $now > $timestamp )
{
$difference = $now - $timestamp;
$string = 'ago';
}
else
{
$difference = $timestamp - $now;
$string = 'from now';
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0, $j_end = count($lengths)-1; $difference >= $lengths[$j] && ($j < $j_end); $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
$interval = $periods[$j] . ( ($difference != 1) ? 's' : '');
$text = "$difference $interval {$string}<br />";
return $text;
}
|
|
The Following User Says Thank You to UseLess For This Useful Post:
|
|
|

June 16th, 2013, 02:38 AM
|
|
Registered User
|
|
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
thank you so much...
PHP Code:
well, Thanks for help,
i got error of negative value in localhost only when i uploaded it to internet its working perfactly
thanks
|
|
 |