helpers.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tatu
  5. * Date: 6/23/20
  6. * Time: 12:10 AM
  7. */
  8. use App\Models\AppSession;
  9. if(!function_exists('get_current_session')) {
  10. function get_current_session(){
  11. return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
  12. }
  13. }
  14. if(!function_exists('friendly_date_time')) {
  15. function friendly_date_time($value, $includeTime = true) {
  16. if(!$value || empty($value)) return "-";
  17. try {
  18. $result = strtotime($value);
  19. $result = date("j M o" . ($includeTime ? ", H:i" : ""), $result);
  20. return $result;
  21. }
  22. catch (Exception $e) {
  23. return $value;
  24. }
  25. }
  26. }
  27. if(!function_exists('friendly_month')) {
  28. function friendly_month($value) {
  29. if(!$value || empty($value)) return "-";
  30. try {
  31. $result = strtotime($value);
  32. $result = date("M o", $result);
  33. return $result;
  34. }
  35. catch (Exception $e) {
  36. return $value;
  37. }
  38. }
  39. }
  40. if(!function_exists('time_in_hrminsec')) {
  41. function time_in_hrminsec($value, $default = '-') {
  42. if(!$value || empty($value)) return $default;
  43. $value = intval($value);
  44. $minutes = intval($value / 60);
  45. $seconds = $value % 60;
  46. $hours = 0;
  47. if($minutes >= 60) {
  48. $hours = intval($minutes / 60);
  49. $minutes = $minutes % 60;
  50. }
  51. $output = [];
  52. if($hours > 0) {
  53. $output[] = "{$hours}h";
  54. }
  55. if($minutes > 0) {
  56. $output[] = "{$minutes}m";
  57. }
  58. if($seconds > 0) {
  59. $output[] = "{$seconds}s";
  60. }
  61. return implode(" ", $output);
  62. }
  63. }