Advertisement
Guest User

chat logger last

a guest
Dec 15th, 2014
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. <?php
  2.  
  3. /* Write a PHP program that receives several messages and the current date, sorts those messages by date and prints the time from the last message to the current date.
  4.  
  5. The messages will be in the format [message] / [date], where date is in the format dd-mm-YYYY hours:min:secs. The messages should be sorted by date and their text should be printed in <div></div> tags on a separate line. For example we are given the current date 12-08-2014 10:14:23 and the following messages:
  6. Thanks :) / 11-08-2014 22:54:52
  7. Hey John, happy birthday! / 10-08-2014 00:00:00
  8. After sorting the messages by date in ascending order (from oldest to most recent), the result is:
  9. <div>Hey John, happy birthday!</div>
  10. <div>Thanks :)</div>
  11. Finally, the time since the most recent message should be printed in the following way:
  12. • a few moments ago if it was less than 1 minute ago
  13. • [full minutes] minute(s) ago if it was less than 1 hour ago
  14. • [full hours] hour(s) ago if it was less than 24 hours ago on the same day
  15. • yesterday if it was the previous date, regardless of the time difference
  16. • [date] if it was earlier than yesterday, where date is in the format dd-mm-YYYY
  17. The resulting timestamp should be printed as follows: <p>Last active: <time>[timestamp]</time></p>. In this case, the difference between the current date and the last message is 11 full hours, but since the message's day is 1 day before the current day, we print yesterday:
  18. <p>Last active: <time>yesterday</time></p>
  19. Input
  20. The input will be read from an HTTP GET request. The current time will come from input text field 'currentTime'. The messages come from a textarea 'messages', holding each message on a separate line.
  21. Output
  22. Each message should be printed in its own <div></div> tags, on a separate line. On the final line, the difference between the current date and the last message date should be printed in the format described above. Ensure the HTML special characters are correctly encoded (use htmlspecialchars() in PHP).
  23. Constraints
  24. • All input dates will be in format dd-mm-YYYY hours:min:secs.
  25. • The received messages will always be in the format [message] / [date].
  26. • There will be exactly one message on a single line.
  27. • There will be no messages with the same date.
  28. • The current date will always be after any message's date.
  29. */
  30. date_default_timezone_set('UTC');
  31. //$_GET['currentTime'] = '29-08-2014 17:33:11';
  32. //$_GET['messages'] = 'Wanna grab some snacks? / 24-08-2014 16:33:12
  33. //Sure, be right there in a minute! / 25-08-2014 22:06:13
  34. //';
  35.  
  36. $inputDate = $_GET["currentTime"];
  37. $message = $_GET["messages"];
  38.  
  39. $currTime = strtotime($inputDate);
  40. $messagesStr = array_filter(explode("\r\n",$message));
  41. $dateText = [];
  42. for($i = 0; $i < sizeof($messagesStr); $i++){
  43. $messages = preg_split("/\s+\/\s+/", $messagesStr[$i], -1, PREG_SPLIT_NO_EMPTY);
  44.  
  45. $dateText[$messages[1]] = $messages[0];
  46. }
  47. ksort($dateText);
  48. $valueArr = array_values($dateText);
  49. for($i = 0; $i < sizeof($valueArr); $i++){
  50. echo "<div>".htmlspecialchars($valueArr[$i])."</div>\r\n";
  51. }
  52.  
  53. $keyArr = array_keys($dateText);
  54. $latestTime = $keyArr[sizeof($keyArr)-1];
  55. $latestTime = strtotime($latestTime);
  56.  
  57. $lastActive = lastActive($currTime, $latestTime);
  58. echo "<p>Last active: <time>".htmlspecialchars($lastActive)."</time></p>";
  59.  
  60. function lastActive($now, $last){
  61. $timeDiff = $now - $last;
  62. $time = '';
  63. $nowD = date('z', $now);
  64. $lastD = date('z', $last);
  65. if($nowD - $lastD > 1){
  66. $time = date("d-m-Y", $last);
  67. } elseif($nowD - $lastD == 1){
  68. $time = "yesterday";
  69. }elseif($nowD - $lastD < 1){
  70. if($timeDiff < 60){
  71. $time = "a few moments ago";
  72. }elseif($timeDiff >= 60 && $timeDiff < 60*60){
  73. $minutes = floor($timeDiff / 60);
  74. $time = "$minutes minute(s) ago";
  75. }elseif($timeDiff >= 60*60){
  76. $hours = floor($timeDiff / (60*60));
  77. $time = "$hours hour(s) ago";
  78. }
  79. }
  80. return $time;
  81. }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement