Jump to content

[SOLVED] function gettime not working


runnerjp

Recommended Posts

i have created a function to get the time of a post and display the relavent text

 

function getTime($dbtime) {
// Get current timestamp
$intTime = time();
// Calculate difference
$intDiff = $intTime - $dbtime;

// Check time
switch($intDiff)
{
         case ($intDiff < 60):
                echo "<strong>Less than a minute ago</strong>";
                break;
        case ($intDiff < 3600):
                echo "<strong>Less than an hour ago</strong>";
                break;
        case ($intDiff < 7200):
                echo "<strong>One hour ago</strong>";
                                break;
        case ($intDiff < 10800):
                echo "<strong>Two hours ago</strong>";
                                break;
                case ($intDiff < 86400):
                echo "<strong>Today</strong>";
                                break;
                case ($intDiff < 172800):
                echo "<strong>Yesterday</strong>";
                break;
        default:
               echo $time;
}
}

 

but when i call it like so

 

<?

$dbtime=$getthreads3['lastrepliedto']; $time = date("F j, Y, g:i a", $dbtime);

 

getTime($dbtime);

?>

 

i get a blank page

Link to comment
https://forums.phpfreaks.com/topic/131337-solved-function-gettime-not-working/
Share on other sites

Your switch statement is incorrect. You need to switch on the value "true" if you're going to use case statements like that.

<?php
switch(true)
{
         case ($intDiff < 60):
                echo "<strong>Less than a minute ago</strong>";
                break;
        case ($intDiff < 3600):
                echo "<strong>Less than an hour ago</strong>";
                break;
        case ($intDiff < 7200):
                echo "<strong>One hour ago</strong>";
                                break;
        case ($intDiff < 10800):
                echo "<strong>Two hours ago</strong>";
                                break;
                case ($intDiff < 86400):
                echo "<strong>Today</strong>";
                                break;
                case ($intDiff < 172800):
                echo "<strong>Yesterday</strong>";
                break;
        default:
               echo $time;
}
?>

 

Ken

ok  here is where im at:

 

im calling it like so..

<?
$dbtime=$getthreads3['lastrepliedto']; $time = date("F j, Y, g:i a", $dbtime); 

getTime($dbtime,$time);
?>

 

and my function is set up now as

 

<?php /**
* get time in forum
*
* @access	public
* @param	none
* @return	string
*/


function getTime($dbtime,$time)  {
// Get current timestamp
$intTime = time();
// Calculate difference
$intDiff = $intTime - $dbtime;

// Check time
switch(true)
{
         case ($intDiff < 60):
                echo "<strong>Less than a minute ago</strong>";
                break;
        case ($intDiff < 3600):
                echo "<strong>Less than an hour ago</strong>";
                break;
        case ($intDiff < 7200):
                echo "<strong>One hour ago</strong>";
                                break;
        case ($intDiff < 10800):
                echo "<strong>Two hours ago</strong>";
                                break;
                case ($intDiff < 86400):
                echo "<strong>Today</strong>";
                                break;
                case ($intDiff < 172800):
                echo "<strong>Yesterday</strong>";
                break;
        default:
               echo $time;
}} ?>

 

but im getting the error Missing argument 2 for getTime()

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.