Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 2 The purpose of this lab is to work with functions and time stamps. Start w

ID: 3782338 • Letter: L

Question

Lab 2

The purpose of this lab is to work with functions and time stamps.

Start with the php code below the question:


• Using string functions, clean up the $author variable and assign it to
$formattedAuthor

   The value of $formattedAuthor should be Gary Tong

• Using date functions, represent the current time in the format of Wednesday
January the 20th, 2016
and assign it to $formattedCurrentTime.

   Use current time, not the hard coded date above!

• Using date functions, represent the $postedTime timestamp in the format of
Wednesday January the 20th, 2016 and assign it to
$formattedPostedTime.

   Use $postedTime time, not the hard coded date above!


Define your own function at the top of file called moments that takes in one parameter:


• the parameter passed in will be in number of seconds

• this function will use conditional statements that check how many seconds were passed
in, and return a string representing this moment. ie.

   if the parameter has less than a month’s worth of seconds,
   return "within the month"

   else all other cases, return "a while ago"


Finally, the $moment variable should be assigned the return value of moments(). When
calling the moments() function, pass it the difference in seconds between now and the
$postedTime.

--------------------------------------------------------------

Lab1.php

<?php

// do not modify these variables
$postedTime = 1481808630;
$author = " gary TonG ";
$content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.";

// modify these variables
$formattedAuthor = "John Doe";
$formattedCurrentTime = "Wednesday January the 20th, 2016.";
$formattedPostedTime = "Wednesday January the 20th, 2016.";
$moment = "just now";

// there is no need to modify the HTML below
?>

<!DOCTYPE html>
<html>
<head>
<title>COMP 3015</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="css/style.css" rel="stylesheet">
</head>
<body>

<div id="wrapper">

<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h3 class="login-panel text-center text-muted">It is now <?php echo $formattedCurrentTime; ?></h3>
<div class="panel panel-info">
<div class="panel-heading">
<span>
First Post!
</span>
<span class="pull-right text-muted">
<?php echo $moment; ?>
</span>
</div>
<div class="panel-body">
<p class="text-muted">Posted on
<?php echo $formattedPostedTime; ?>
</p>
<p>
<?php echo $content; ?>
</p>
</div>
<div class="panel-footer">
<p> By
<?php echo $formattedAuthor; ?>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

Explanation / Answer

<?php

function moments($numberOfSecs) {
if ($numberOfSecs < 2592000) { //number of seconds for a month consisting of 30 days
       return "within the month";
   }else{
       return "a while ago";
   }
      
   return;
}

// do not modify these variables
$postedTime = 1481808630;
$author = " gary TonG ";
$content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.";
// modify these variables
$formattedAuthor = trim($author);
$formattedCurrentTime = date("l F \t\h\e jS, Y") ;
$formattedPostedTime = date("l F \t\h\e jS, Y", $postedTime);
//echo time() - $postedTime;
$moment = moments(time() - $postedTime);

// there is no need to modify the HTML below
?>
<!DOCTYPE html>
<html>
<head>
<title>COMP 3015</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h3 class="login-panel text-center text-muted">It is now <?php echo $formattedCurrentTime; ?></h3>
<div class="panel panel-info">
<div class="panel-heading">
<span>
First Post!
</span>
<span class="pull-right text-muted">
<?php echo $moment; ?>
</span>
</div>
<div class="panel-body">
<p class="text-muted">Posted on
<?php echo $formattedPostedTime; ?>
</p>
<p>
<?php echo $content; ?>
</p>
</div>
<div class="panel-footer">
<p> By
<?php echo $formattedAuthor; ?>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>