Advertisement
Guest User

chat logger

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