Jump to content

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()

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.