I'm trying to make strtotime give me a timestamp, but I only know that it's a hour and minute and it's the next in the row. Fx.
If the given hour and minute is 01:00 (AM) I require it to return the timestamp of 01:00 (AM), if it is the day before at 19:00, I would like it to return the timestamp of 01:00 the next day, but if it is after midnight at 00:30, it should return the timestamp 01:00 the same date.
To simplify it, it's the NEXT occurrence of a time.
Strtotime is giving me the time on the same day, if and not the next occurrence.
You can easily generate a timestamp, see if it's already past, and then increment the day.
Something like this perhaps?
$timestamp = strtotime("1:00 am");
if($timestamp < time()) {
$timestamp = strtotime("+1 day",$timestamp);
}
Or if you want to tighten it up to a one-liner:
$string = "1:00 am";
$timestamp = (strtotime($string) > time()) ? strtotime($string) : strtotime("tomorrow " . $string);
Related
I've spent so much time on trying to figure out how to create a loop that will echo time between given $start_time and $end_time incrementing it by one hour in format HH:MM. For example if i have $start_time = "22:00" and $end_time="04:00" the output should be like:
22:00
23:00
00:00
01:00
02:00
03:00
04:00
Any idea how to do it?
This is not a very elegant solution but you could do something like this:
<?php
$start_time = "22:00";
$end_time="04:00";
$timestamp = strtotime($start_time);
while(date('H:i', $timestamp) !== date('H:i', strtotime($end_time) + 60*60)){
echo date('H:i', $timestamp) . "\n";
$timestamp += 60*60;
}
With date function with 'H:i' parameter, you can compare only the hour part of the timestamp. That way the for loop will add an hour after every iteration until the variable has passed the end_time (that ensures you echoe the last time too).
I am trying to compare two times one of which is in 12 hour format and the other is in 24 hour format, that is after 12am.
$time = date( 'H:i:s', current_time( 'timestamp', 0 ));
$open = '18:00';
$closed = '01:30';
if ( $time < $open || $time > $closed)
{
//do something
}
this is always failing it is something to do with the 01.30 because if I do anything less than 00:00 e.g. 23.30:
$time = date( 'H:i:s', current_time( 'timestamp', 0 ));
$open = '18:00';
$closed = '23:30';
The above works.
I have also tried strtotime like this without success.
if ( $time < strtotime($open) || $time > strtotime($closed))
How can I evaluate between 6pm and 2am in the morning?
Much like when working with months and January being either the first month of this year or 13th month of last year, 1 am is either the 1st hour of today or the 25th hour of tomorrow.
I'd reverse your logic and determine if it is currently greater than 0130 and less than 1800. Take out the colons and it makes it really easy
$close=013000;
$open=180000;
$now=date( 'His', time());
if (($close<$now)&&($now<$open)){
print("Go away, we're closed");
}else{
print("Welcome to the store!");
}
If you don't want to reverse the logic, then instead of using 01:30 use 25:30 for the value - PHP and date are pretty good about rolling over the next position when a larger than max value is used some place.
Since you are only comparing the current time (time right now), logically you don't have to worry about tomorrow's time. You only need to compare if you are inside/outside today's opening/closing times.
<?php
// SET RELEVANT TIMEZONE
date_default_timezone_set('Europe/Dublin');
// CURRENT UNIX TIMESTAMP
$time_now = time();
// TODAY AT 18:00:00 (24 HOUR) UNIX TIMESTAMP
$opening_time = DateTime::createFromFormat('H:i:s', '18:00:00')->format("d-M-Y H:i:s"); // 11-May-2018 18:00:00
// TODAY AT 01:30:00 (24 HOUR) UNIX TIMESTAMP
$closing_time = DateTime::createFromFormat('H:i:s', '01:30:00')->format("d-M-Y H:i:s"); // 11-May-2018 01:30:00
// WE ARE CLOSED IF:
// TIME NOW IS AFTER CLOSING TIME TODAY (01:30:00)
// AND TIME NOW IS BEFORE OPENING TIME TODAY (18:00:00)
if($time_now > strtotime($closing_time) && $time_now < strtotime($opening_time))
{
echo "Sorry, we are closed!";
}
else
{
echo "We are open, come on in!";
}
Hi Suppose I have a timestamp of "2005-10-16 13:05:41".
How would I go about creating a variable that will have a unixtime of the next time it becomes 10am from that initial point?
Would it be something like this?
$timestamp = "2005-10-16 13:05:41";
$tenAMTime = strtotime("next 10am", $timestamp);
I am guessing there is some string I can use to do this? Like "next thursday" example in the PHP documentation.
You nearly had it...
$tomorrowAt10Am = strtotime('+1 day 10:00:00', $timestamp);
Edit:
This was based on the title of your question, for the timestamp of 10am the next day. If you want to output 10am the same day for any times before 10am then you'll want to add some extra logic, as thatidiotguy suggested.
Edit2:
For some reason it won't work if you put all the logic in the same strtotime method, so I made a simple function. You could easily put this into a single line, but I left it as 2 to make it clearer:
$time1 = strtotime('-2 days 09:59:59');
$time2 = strtotime('-2 days 10:00:01');
function next_10am($time)
{
$temp = strtotime('+1 day -10 hours', $time);
return strtotime('10:00', $temp);
}
echo next_10am($time1); // Outputs: 2012-09-08 10:00:00
echo next_10am($time2); // Outputs: 2012-09-09 10:00:00
There is no way for strtotime to know whether or not 10am has already passed, so this is how I would do it:
$timestamp = strtotime("2005-10-16 13:05:41");
// Get current hour and if it is > 10 add a day
if (date('G',$timestamp) >= 10) {
$tenAMTime = strtotime("+1 day 10am", $timestamp);
}
else {
$tenAMTime = strtotime("10am", $timestamp);
}
echo date('r',$tenAMTime); // Comment this out if you want
There is a simple way to get unix time range of a day if given a random timestamp from that day ?
I have a date like 1345547471 which is "Tue, 21 Aug 2012 11:11:11 GMT"
There is a php function that can receive a timestamp like this and return a 00:00 hours timestamp and a 23:59 hours timestamp of that day ?
Thank you.
Sure, DateTime can do that:
$time = 1345547471;
$date = new DateTime;
// $date->setTimezone( new DateTimeZone( "America/New_York")); // Can set TZ here if needed
$date->setTimestamp( $time);
Now, you can set the time to whatever you want:
$date->setTime( 0, 0, 0); // 0h 0m 0s
And grab the resulting UNIX Timestamp:
$timestamp = $date->getTimestamp();
Same thing for the next use-case:
$date->setTime( 23, 59, 0);
$timestamp = $date->getTimestamp();
It is important to note that DateTime will properly handle cases of daylight savings time and local time discontinuities.
You can use the mod (gives the remainder after a division) PHP function like this to get the first second of a Unix timestamp (ie, today 0:00:00)
$var=time()-(time()%86400);
Then with this unix timstamp, you can add 86399 to get the last second of the day.
Edit: This doesn't account for dalylight savings.
$ts = 1345547471;
$ts_00_00 = mktime(0,0,0, date("m", $ts), date("d",$ts), date("Y",$ts);
$ts_23_59 = mktime(23,59,59, date("m", $ts), date("d",$ts), date("Y",$ts);
Documentation:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.mktime.php
If you are using PHP >= 5.3.0 Then you can use this...
Check out for this.
http://www.php.net/manual/en/datetime.createfromformat.php
This is similar to Fluffeh's answer, but accounts for daylight savings time. This is based on the server's time zone.
//Get time range for today
$start = strtotime(date("Y-m-d")." 00:00:00");
$end = strtotime(date("Y-m-d")." 23:59:59");
//Show our date in a human-readable format for troubleshooting
echo date(DATE_RFC1036,$start)."<br>".date(DATE_RFC1036,$end);
If you want to specify a custom timezone instead of the server timezine, you can add it to like so:
//Get time range for today
$start = strtotime(date("Y-m-d")." 00:00:00 -0500");
$end = strtotime(date("Y-m-d")." 23:59:59 -0500");
//Show our date in a human-readable format for troubleshooting
echo date(DATE_RFC1036,$start)."<br>".date(DATE_RFC1036,$end);
Link to working Sample
so i have a date format like 07-09-10 and i want to know how to get ago from that date and if i can have a conditional like
if(is_date_with_1_week_of_above_date){
//do something
}
For checking date with relation to the Current Timestamp
if( strtotime( '-1 week' )>=$dateToCheck ) {
# $dateToCheck is within the last week
}
The other responses have good solutions for simple checking whether two date/times are within 1 week of each other - no point me repeating them.
Your date doesn't make clear the format (is it MM-DD-YY, DD-MM-YY, YY-MM-DD, etc.)? But an example using ISO 8601 date format is this:
$oneWeekAgo = strftime("%Y-%m-%d", strtotime("2010-07-09") - 60*60*24*7);
For a comparison, you can use the UNIX timestamp values
$date = "2010-07-09";
$compareDate = "2010-07-03";
$curTimestamp = strtotime($date);
$compareTimestamp = strtotime($compareDate);
if(abs($curTimestamp - $compareTimestamp) < 60*60*24*7)
{
// within 1 week
}
Edit
Per the comment on the date format, dd-mm-yy is a recognized format for dates, but mm-dd-yy is not in strtotime as seen here:
http://www.php.net/manual/en/datetime.formats.date.php
For it to work, you'd have to convert the dashes to slashes.
Also, if you're looking if the date is specifically one week prior,
$date = str_replace('-','/',"07-10-10");
$compareDate = str_replace('-','/',"07-03-10");
$curDate = strftime("%m/%d/%y", strtotime($date));
$compareDate = strftime("%m/%d/%y", strtotime($compareDate) + 60*60*24*7);
if($curDate == $compareDate)
{
// is one week prior
echo "OK";
}
Depending on your Time Zone, some days might have only 23 hours, so you can not use as a rule that a day has (60 seconds * 60 minutes * 24 hours) and with this, to calculate a specific date.
Specifying a date:
$specific_date = date( "Y-m-d" ); // for today
or
$specific_date = date( "Y-m-d", $timestamp ); // where timestamp is: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
The answer will be
$date = strtotime( $specific_date . " -1 week" );