From 92d1a48d815870ee4244122a017d013b0a07a9e7 Mon Sep 17 00:00:00 2001 From: Megat Zulkhairi Megat Mukhtar Date: Mon, 11 Oct 2021 17:36:08 +0800 Subject: [PATCH] Create config file - set hour greeting - show date time greeting - format date time. --- config/greet.php | 23 ++++++++++++++++++++++- src/Greet.php | 43 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/config/greet.php b/config/greet.php index ca5d8ed..a6886c9 100644 --- a/config/greet.php +++ b/config/greet.php @@ -1,5 +1,26 @@ [ + 'morning' => [ + 'end' => 12, + 'text' => 'Good morning' + ], + 'afternoon' => [ + 'start' => 12, + 'end' => 17, + 'text' => 'Good afternoon' + ], + 'evening' => [ + 'start' => 17, + 'end' => 19, + 'text' => 'Good evening' + ], + 'night' => [ + 'start' => 19, + 'text' => 'Good night' + ], + ], + 'show_datetime' => true, + 'format_datetime' => 'd M Y, H:i:sa' ]; diff --git a/src/Greet.php b/src/Greet.php index 61544d5..49f32f3 100644 --- a/src/Greet.php +++ b/src/Greet.php @@ -8,18 +8,43 @@ public static function hello($name = null) { $time = date('H'); - if ($time < '12') { - echo 'Good morning'; - } elseif ($time >= '12' && $time < '17') { - echo 'Good afternoon'; - } elseif ($time >= '17' && $time < '19') { - echo 'Good evening'; - } elseif ($time >= '19') { - echo 'Good night'; + $greets = config('greet.hour'); + if (is_array($greets)) { + foreach ($greets as $key => $value) { + if (!isset($value['start'])) { + if ($time < $value['end']) { + echo $value['text']; + } + } else if (!isset($value['end'])) { + if ($time >= $value['start']) { + echo $value['text']; + } + } else { + if ($time >= $value['start'] && $time < $value['end']) { + echo $value['text']; + } + } + } } + // if ($time < '12') { + // echo 'Good morning'; + // } elseif ($time >= '12' && $time < '17') { + // echo 'Good afternoon'; + // } elseif ($time >= '17' && $time < '19') { + // echo 'Good evening'; + // } elseif ($time >= '19') { + // echo 'Good night'; + // } + if ($name != null) { - echo ', '.$name; + echo ' ' . $name . '.'; + } + + $show = config('greet.show_datetime'); + if ($show === true) { + $format = config('greet.format_datetime'); + echo ' Today is ' . date($format); } } }