helpers.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, $default = '-') {
  16. if(!$value || empty($value)) return $default;
  17. try {
  18. $result = strtotime($value);
  19. $result = date("jS 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_time')) {
  28. function friendly_time($value, $default = '-') {
  29. if(!$value || empty($value)) return $default;
  30. try {
  31. $result = strtotime($value);
  32. $result = date("H:i", $result);
  33. return $result;
  34. }
  35. catch (Exception $e) {
  36. return $value;
  37. }
  38. }
  39. }
  40. if(!function_exists('friendly_month')) {
  41. function friendly_month($value) {
  42. if(!$value || empty($value)) return "-";
  43. try {
  44. $result = strtotime($value);
  45. $result = date("M o", $result);
  46. return $result;
  47. }
  48. catch (Exception $e) {
  49. return $value;
  50. }
  51. }
  52. }
  53. if(!function_exists('time_in_hrminsec')) {
  54. function time_in_hrminsec($value, $default = '-') {
  55. if(!$value || empty($value)) return $default;
  56. $value = intval($value);
  57. $minutes = intval($value / 60);
  58. $seconds = $value % 60;
  59. $hours = 0;
  60. if($minutes >= 60) {
  61. $hours = intval($minutes / 60);
  62. $minutes = $minutes % 60;
  63. }
  64. $output = [];
  65. if($hours > 0) {
  66. $output[] = "{$hours}h";
  67. }
  68. if($minutes > 0) {
  69. $output[] = "{$minutes}m";
  70. }
  71. if($seconds > 0) {
  72. $output[] = "{$seconds}s";
  73. }
  74. return implode(" ", $output);
  75. }
  76. }
  77. if(!function_exists('sanitize_field_name')) {
  78. function sanitize_field_name($name) {
  79. $result = strtolower($name);
  80. return preg_replace("/[^0-9a-z]/i", "_", $result);
  81. }
  82. }