Write a program that will input your name and birthday and will display the Date
and time of your input and display also your Age.
Also the system will greet you Good Morning / Afternoon/Evening.
The system will also inform you that you are young, very young, old, or very old
condition:
if age < 15 very young
age > 15 and < 20 young
age >= 20 and < 50 old
age >= 50 very old
im stuck on the condition of the machine determining whether i'm very young, young and old
if i run this code i always get the error of DateInterval could not be converted to int i'm at a loss of what to do
if (isset($_GET['submit'])) {
$result1= $_GET['Name'];
$result2= $_GET['Bday'];
$birthDate = $result2;
$currentDate = date("d-m-Y");
$age = date_diff(date_create($birthDate), date_create($currentDate));
date_default_timezone_set("Asia/Kolkata");
$h = date('G');
if($h>=5 && $h<=11)
{
echo "Good Morning $result1 ... <br>You are ".$age->format("%y years old, %m months and");
}
else if($h>=12 && $h<=15)
{
echo "Good Afternoon $result1 ... <br>You are ".$age->format("%y years old, %m months");
}
else
{
echo "Good Evening $result1... <br>You are ".$age->format("%y years old, %m months and");
}
if ($age < 15 )
{
echo "you are very young";
}
else if($age < 50)
{
echo "you are young";
}
else if($age < 51 )
{
echo "you are old";
}
else if ($age >= 50 )
{
echo "you are very old";
}
Related
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
How to convert hh:mm:ss to minutes
(7 answers)
Closed 4 years ago.
$time_frame = floor(abs((strtotime($notification['note_date'])-strtotime(date("Y-m-d H:i:s")))/60/60));
if($time_frame>24){
$time_frame = floor($time_frame/24);
if($time_frame>1){
$time_frame = $time_frame." days ago";
} else{
$time_frame = $time_frame." day ago";
}
} else if($time_frame>1) {
$time_frame = $time_frame." hours ago";
} else if($time_frame==1) {
$time_frame = $time_frame." hour ago";
} else{
$time_frame = "1 hour ago"; //I want to break this hour in to minutes
}
How do i break that hour in to display in to minutes, last else statement.
I recommend dealing with seconds/timestamps or date-time objects instead - this would be a better and more dynamic approach in my opinion. Perhaps have a look at this question instead Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
That said, if you want to do it with your current approach, you can multiply your variable by 60 (as there is 60 minutes in an hour), such as
$time_frame = 60*$timeframe;
Examples,
If $timeframe is 1, then you have exactly one hour. 60min * 1 = 60 minutes.
If $timeframe is 0.5, that would be half an hour. 60min * 0.5 = 30 minutes.
If $timeframe is 0.25, means that 15 minutes have passed, and 60min * 0.25 = 15 minutes.
You might want to round that number to your liking, so that you will not get output such as 1.43 minutes left. Also note that floating point numbers may not be exactly accurate, hence my recommendation of using datetime objects or timestamps instead.
If you use the DateTime class, you can use the diff() method so you don't have to mess with all the calculating. diff() returns a DateInterval which has public properties you can use to determine the appropriate message.
$interval = date_create($notification['note_date'])->diff(new DateTime);
if ($interval->days > 1) {
$time_frame = "{$interval->days} days";
} elseif ($interval->days == 1) {
$time_frame = "1 day";
} elseif ($interval->h > 1) {
$time_frame = "{$interval->h} hours";
} elseif ($interval->h == 1) {
$time_frame = "1 hour";
} elseif ($interval->i > 1) {
$time_frame = "{$interval->i} minutes";
} elseif ($interval->i == 1) {
$time_frame = "1 minute";
} else {
$time_frame = "Less than 1 minute";
}
echo "{$time_frame} ago";
You can actually USE that string! :-D
$x = new DateTime('1 hour ago'); // done at 17:17
echo $x->format('Y-m-d H:i:s'); // outputs 2018-10-23 16:17:12
https://3v4l.org/jWTC8
I am writing a small php program that greets according to the time of day on the server. The rules are as follows:
Between 3:00:00 AM and 11:59:59 AM say Good morning!.
Between 12:00:00 PM and 4:59:59 PM say Good afternoon!.
Between 5:00:00 PM and 2:59:59 AM say Good evening!.
First, to simply matters, I decide to use military time. My program is as follows:
function greetingWord()
$hour = date("G");
if($hour >= 15 && $hour < 24)
{
echo "<p>Good Morning. Today is: </p>";
} else if($hour >= 12 && $hour < 17)
{
echo "<p>Good afternoon. Today is: </p>";
}
else if($hour >= 17 && $hour < 3)
{
echo "<p>Good evening. Today is: </p>";
}
}
my question regards that last else if. I suspect there is something wrong with my logic there so I'd apperciate it if someone would help me out with that final condition, the case of it being between 5pm and 2:59am.
Thank you!
DB
I think the easiest way to do it is leaving the last condition with a simple "else", so:
function greetingWord(){
$hour = date("G");
if($hour > 0 && $hour < 24){
if($hour >= 3 && $hour < 12)
{
echo "<p>Good Morning. Today is: </p>";
}else if($hour >= 12 && $hour < 17){
echo "<p>Good afternoon. Today is: </p>";
}else{
echo "<p>Good evening. Today is: </p>";
}
}
}
Hope it works.
I used ternary operators to display a part of day with a small modification of the code.
function greetingWord(int $hour): string
{
return ($hour >= 3 && $hour < 12)
? "Morning: "
: (($hour >= 12 && $hour < 17) ? "afternoon: " : "evening:");
}
foreach(range(0, 23) as $hour) {
echo "<p> Good " . greetingWord($hour) . " Today is: ". $hour ."</p>\n";
}
//or
echo "<p> Good " . greetingWord((int) date('G')) . " Today is: ". $hour ."</p>\n";
$logintime value 1 year finished means, it will showing 1 years ago, but suppose 2 months only finished means I want to show 2 months ago, but my code showing like 60 days ago, I don't know where I did mistake, remaining hour, minutes this are working fine, only month making problem, $logintime = 2016-02-27 03:00:00
function timeAgo($logintime) {
date_default_timezone_set('UTC');
date_default_timezone_set('Asia/Kolkata');
$start_date = new DateTime($logintime);
$since_start = $start_date->diff(new DateTime(date("Y-m-d h:i:s")));
if (intval($since_start->format('%Y') ) >= 1) {
echo $year = $since_start->format('%Y years ago');
} else if (intval($since_start->format('%m')) >= 12) {
echo $months = $since_start->format('%m month ago');
} else if (intval($since_start->format('%a')) >= 1) {
echo $days = $since_start->format('%a days ago');
} else if (intval($since_start->format('%h')) >= 1) {
echo $hourss = $since_start->format('%h hours ago');
} else if (intval($since_start->format('%i')) >= 1) {
echo $min = $since_start->format('%i minuts ago');
} else if (intval($since_start->format('%s')) >= 1) {
echo $min = $since_start->format('%s seconds ago');
}
}
Your this line of code :
else if(intval($since_start->format('%m')) >= 12){
It says if the month > = 12, then show months ago, but you just have 2 months.
So you should consider changing it to :
else if(intval($since_start->format('%m')) >= 1){
I have this code which is working fine for some dates which I don't understand why, below code should calculate the total days. If I select leavefrom = 2014-04-21 and leaveto = 2014-05-02, it gives me total of 8 days but it should be 9 nine days.
Here is the calendar:-
function total_day($leavefrom, $leaveto){
$start_date=strtotime($leavefrom);
$cur_day=$start_date;
$end_day=strtotime($leaveto);
$count=0;
$holiday=array("2014-05-01"=>"Labour Day", "2014-08-31"=>"Independence Day", "2014-12-25"=>"Christmas");
while(1){
//echo date("Y/m/d", $cur_day)."<br/>";
$cur_day=$cur_day +(3600*24);
//echo $count."S--".date("Y-m-d", $cur_day)."<-----E--".$end_day;
$day_of_week=date('w', $cur_day);
//echo "day_of_week-----".$day_of_week."<br/>";
if ($day_of_week == 0 || $day_of_week == 6) {
//No Operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
//echo "Holiday because of ".$holiday[date("Y-m-d", $cur_day)];
}else{
$count++;
}
//echo "Total day--".$count."<br/><br/>";
if(($cur_day==($end_day+(3600*24)))||($cur_day>$end_day)){
break;
}
}
//$count = $count + 1;
return $count;
}
$totaldays = total_day($leavefrom, $leaveto);
I see no error there. From 2014-04-21 to 2014-05-02 there are 11 days out of which 2 weekends and 1 holiday (2014-05-01). Thus making 11 days - 3 days = 8 days
But If you want to include the starting date as well. Then you can use this:
while(1){
if($cur_day>$end_day){
break;
}
$day_of_week=date('l', $cur_day);
if (in_array($day_of_week,array('Saturday','Sunday'))) {
// NO operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
// NO operation
}else{
$count++;
}
$cur_day=$cur_day +(3600*24);
}
I am trying to write a function which checks if a "Finished Lesson" was four days ago. How do I check if said lesson was in that time range, for example. If it was finished yesterday, 2 days ago, 3 days ago, 4 days ago, it would be true since it is in the time range of "4 days ago".
How do I check this?
So far I've done:
$time = time();
$fourDays = 345600;
$threeDays = 259200;
$lastLesson = $ml->getLesson($cid, $time, true);
$lastLessonDate = $lastLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate + $fourDays < $time)
{
$displayLastLesson = true;
//We print lesson that was finished less than 4 days ago
}
else
{
//We print lesson that is in the next 3 days
}
Right now, the if statement keeps hitting true which is not what I want since I have a lesson that was finished on the 3rd May. It should be true for a lesson that was finished on the 7th May I guess?
$time = time();
$fourDays = strtotime('-4 days');
$lastLesson = $ml->getLesson($cid, $time, true);
$lastLessonDate = $finishedLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate >= $fourDays && $lastLessonDate <= $time)
{
$displayLastLesson = true;
//We print lesson that was finished less than 4 days ago
}
else
{
//We print lesson that is in the next 3 days
}
All calculations should be calculated relative to today at 12am, not time() which gives you the current time now (e.g. 6pm) This is an issue because when you do this, 1 day ago (now - 24hours) means time that is between yesterday 6pm and today 6pm. Instead, yesterday should mean a time between yesterday 12am and today 12am.
Below is a simplified calculation to illustrate the idea:
$lastLessonDate = strtotime($lastLessonDate);
$today = strtotime(date('Y-m-d')); // 12:00am today , you can use strtotime('today') too
$day = 24* 60 * 60;
if($lastLessonDate > $today) // last lesson is more than 12:00am today, meaning today
echo 'today';
else if($lastLessonDate > ($today - (1 * $day))
echo 'yesterday';
else if($lastLessonDate > ($today - (2 * $day))
echo '2 days ago';
else if($lastLessonDate > ($today - (3 * $day))
echo '3 days ago';
else if($lastLessonDate > ($today - (4 * $day))
echo '4 days ago';
else
echo 'more than 4 days ago';