This question already has answers here:
How to check if time is between two times in PHP [duplicate]
(2 answers)
Closed 8 years ago.
I'm sure this is a pretty basic task, but I'm quite new to programming and am a bit confused.
Basically I want to execute a different action based on the time of day. For example, if the time is between 05:00 and 20:00 (8pm), run "scriptA". ELSE, or if time is between 20:00 and 05:00, run "scriptB".
What is the easiest way to do this? I basically want to run one script during the "Day" and the other at "Night".
Thank you!
You can try using the date function.
echo date("H:i:s");
will display the current time. Get this string. Compare it and use an if/else.
PHP Date Reference Manual
<?php
if((date("H") > 5) && (date("H")<20) )
{
//run_my_day_script
}
else
{
//run_my_evening_script
}
?>
Get the hour of the day in a 24 hour format, without leading zeros.
$hour = (int)date("G");
Then you can do a less or greater that check.
if ($hour >= 5 && $hour <= 20)
{
// Do Something.
}
http://php.net/manual/en/function.date.php
Related
This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.
I have a list of timestamps that represent a list of backed up files. But to reduce the amount of space needed I only want to keep the files that are from around mid day- I have started writing a function check but got stuck on how could i check if the timestamp is between 12 and 1 for that day? I have a list of timestamps for many days.
function check_date($timestamp='')
{
if (($timestamp < strtotime("-1 week")) && (time is between 12 and 1 )){
}
else
remove
}
I wrote an answer to this previously, I will try find a link in a minute when im free but essentially get the timestamp for 12 and the timestamp for 1 then
if(timestamp12 < curTimestamp && curTimestamp < timeStamp1)
Then you know that the curTimeStamp is between 12 and 1.
Previous answer https://stackoverflow.com/a/11578345/1475461
The other question was for a javascript implentation but timestamp comparison works the same whether you are using PHP, javascript etc. since they are miliseconds/seconds since a set point in time (1st January 1970), so it is just a comparison of integers.
Okay, let me think...ah, here it is. Good old 'localtime' function.
http://www.php.net/manual/de/function.localtime.php
Run your timestamp through this function, then you can check the result.
$TimeInfo = localtime(timestamp, true);
if (($timestamp < strtotime("-1 week")) && $TimeInfo["tm_hour"] == 12) {
}
else remove
With this code, all files, which have arrived at 12:00, up to 12:59 (1 is excluded in this example) are preserved, the others get removed.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert Unix timestamp to hhmmss?
I have a timestamp in the format of:
1330559989
How to check the hours in the timestamp is in between 6 hours and 23 hours?
Can someone help me please?
date('H', 1330559989) will give you the hours in 24h format with a leading zero, in this example: 23.
There is also a specific function for the hour of a timestamp called getdate which offers an array interface to that information (demo):
getdate(1330559989)['hours']; # 23
$hrs=date('H', 1330559989);
if ($hrs>= 6 && $hrs<= 23) {
// your code
}
If you want to check the time between now and that given timestamp, then you can use:
<?php
echo date("H", strtotime(now)-1330559989); // Displays the difference from now.
echo date("H", 1330559989); // Displays that time!
?>
Hope this helps! :)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting timestamp to time ago in php e.g 1 day ago, 2 days ago
I'm trying to display how old the post is through php. What I'm doing is storing in mysql the php timestamp using the code below.
$timestamp=time();
So then I'm left with a number like this
1334216865
How would I go about changing it so it will say example:
8 seconds ago
If you refresh the page 10 seconds later it will show:
18 seconds ago
I'm not sure how I would convert a timestamp back into time to show my examples.
Thank you
Or you could make a little "time_ago" function:
function ago($time) {
$timediff=time()-$time;
$days=intval($timediff/86400);
$remain=$timediff%86400;
$hours=intval($remain/3600);
$remain=$remain%3600;
$mins=intval($remain/60);
$secs=$remain%60;
if ($secs>=0) $timestring = "0m".$secs."s";
if ($mins>0) $timestring = $mins."m".$secs."s";
if ($hours>0) $timestring = $hours."u".$mins."m";
if ($days>0) $timestring = $days."d".$hours."u";
return $timestring;
}
echo ago(1334214962);
echo (time() - $post_timestamp) . " seconds ago";
very simple.
Just run the time() function again and subtract the posted time - then do some arithmetic to output how long it has been: 60 seconds per minute, 60 minutes per hour,etc... the time function is how many seconds since some event a long time ago. so, every increment is 1 second on the time function.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate person's current age
I want to find the persons age.
$time=$row['date_of_birth'];
$date=explode("-", $time);//gets the date birth from the database and puts it into an array
$a=getdate();//gets todays date
$diff_date= gregoriantojd($a["mon"],a["mday"],a["year"] ) - gregoriantojd(date[1],date[2],date[0]);//subtracts the differences between the dates, this is returned in seconds..
The question is whether this is the way to do it? and how do I convert the seconds and convert them to years (as an integer)?!
EDIT:
I think that the last line should be this:
return floor($diff_date /60*60*24*365);
Just try with strtotime (converts date string into timestamp):
$time = strtotime($row['date_of_birth']);
$age = date('Y', $time - time());
echo 'age: ' . $age;
If $time is supposed to be unix timestamp
$time=$row['date_of_birth'];
$date1 = new DateTime($time);
$date_diff = $date1->diff(new DateTime(), true);
$age = $date_diff->y;
I think you can make use of the date_diff function in PHP.
1st question: The question is whether this is the way to do it?
No. The function gregoriantojd (per the manual entry here) returns a value in days, not seconds. I like the date_diff conversion that Piotr gives the best.
2nd question: how do I convert the seconds and convert them to years
(as an integer)?!
Well, if you were starting with a number of seconds (which you aren't) then your second formula would be mostly correct, but of course it wouldn't account for leap-years. So for anyone older than 4, or at least 8, it would be off for one or more days around their birthday. Usually on their birthday is a pretty important time to get their age correct.