PHP date before a specified time - php

I know this has been asked in different ways but I'm struggling to get this to work.
I have a timestamp output from an API call in the following format:
2019-12-02T14:30:00
I'm capturing that in my PHP as follows
$person['StartDateTime']
I'm then formatting the timestamp and storing it into the $thetime variable like below.
$thetime = date("g.i",strtotime(date( $person['StartDateTime'])));
The echoed output of the $thetime to the browser gives me 14.00
Now I want to only have the if statement work if the $thetime holds a time before 14:00, if I try the following it does not work, I just get all of the times before and after.
if(strtotime($thetime) < strtotime('14') ) {
//shows all times before and after, no good...
}
if I do the following this works...
if(strtotime($thetime) < strtotime('now') ) {
//shows all times before 'now' but not what I want...
}
Any help would be much appreciated.

Fixed by formatting the date correctly first...
$starttime = date("H.i",strtotime(date($thetime)));
if(strtotime($thetime) < strtotime('14') ) {
//Will now only display times before 14 (2pm)
}

Related

PHP Remove Content when date hits

I'm trying to have make a simple script which hides content when the date hits.
So when it's 03-11-2016, show the content but when the date is 03-15-2016, hide the content.
if(date(m-d-Y) > "03-15-2016") {
// do stuff
}
Sadly the script below isn't working for some reason, any help would be nice, thanks!
date function returns a string. Comparing strings may cause some unexpected results. What you need to compare is timestamps of dates:
if (strtotime("now") > strtotime("03/15/2016 00:00:00")) {
// do stuff
}
Note, I changed your "03-15-2016" to "03/15/2016" cause it's format, which can be parsed by strtotime function.
If your wanting to maintain the same format as your current script, this will work:
<?php
if(strtotime(date("Y-m-d")) > strtotime("2016-1-04")) {
// do stuff
}
?>
think about using something like:
$today = time(); // time() is now (timestamp) since epoch
$hideIt = strtotime("2016-03-15"); // timestamp of date to hide stuff
if ( $today > $hideIt ) // if today is greater than the date we want to hide things
{
// do stuff...
}
Your syntax is not working because you are using parameters in a wrong order. You need to use them in order by coming first the year, month and the day:
if (date('Y-m-d') > '2016-03-15') {
// do stuff
}

How can I use PHP to display a variable every other day at a certain time?

Context
I would like to use PHP to change out content in HTML every other day. I can currently achieve this, however, I would need the content to change at 7:00 AM PST. Right now, I'm essentially subtracting the current time from a set point in time using mktime and rounding down. A simple if statement takes care of the variable.
The Code
<?php
$first_date = mktime(7,0,0,1,1,2014);
$second_date = time();
$offset = $second_date-$first_date;
$this_day = floor($offset/60/60/24);
if ($this_day % 2 == 0) {
//do something
} else {
//do something else
}
?>
Like I stated before, the code is working however it is not changing at 7:00 AM PST. I tried adjusting the start time in $first_date however it didn't seem to help. I think I am missing something regarding actual timezones and how the time is being calculated. Any insight would be greatly appreciated.
I just take the start date and gave it a timezone, then checked the day number of the year. If it's even and 7am or later, do something. Otherwise, do something else. The only issue you will have is New Years. This may show the same content two days in a row.
<?php
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles'));
if ($date->format('z') % 2 == 0 && $date->format('h') >= 7) {
//do something
} else {
//do something else
}
?>

How to remove to expiry time in php using mysql database

How to remove the expiry time in php using mysql database....
I want to remove the expiry time which are derived from mysql db..In my code i want to compare the show time from database with current time,to get the showtime which is greater than the current time.(Eg), showtime="10:30 AM,2:30 PM,6:30 PM,9:30 PM" and curent time is 03:21 PM.
Getting output:
{"offer_details":[{"showtime":["10:30 AM","2:30 PM","6:30 PM","9:30 PM"]}],"success":1,"message":"Successfully found "}
Expected output:
{"offer_details":[{"showtime":["6:30 PM","9:30 PM"]}],"success":1,"message":"Successfully found "}
$result=mysql_query("select * from offer_details where movieid='$movieid' and date_value='$date_value' and offer_status!='Expired'")or die (mysql_error());
$offer_details=array();
$offer_details['showtime']=explode(',',$row['showtime']);
$av = sizeof($offer_details['showtime']);
echo $showtime[i];
Thanks,
You can compare time via hour like this:
foreach($offer_details['showtime'] as $key=>$showTime){
if(date("H", strtotime($showTime)) < date("H", strtotime("now"))){
unset($offer_details['showtime'][$key]);
}
}
array_values($offer_details['showtime']);
When you explode the showtime string you actually obtain strings which are formatted as times but remain as strings. That is why you cannot get the result when you compare with the current time as strings ("8:00 AM" will always be greater than "3:00 PM"). I suggest you store the showtimes as different fields in your database (with datetime types) so that you only pick those greater than now. I hope it helps.
Try this example and see if this helps.
<?php
$offer_details['showtime']= ["10:30 AM","2:30 PM","6:30 PM","9:30 PM"];
//$now = strtotime('now');
$now = strtotime('3:30 PM');
$showtimes = array();
foreach($offer_details['showtime'] as $showtime) {
if(strtotime($showtime) >= $now) {
$showtimes[] = $showtime;
}
}
print_r($showtimes); //outputs Array ( [0] => 6:30 PM [1] => 9:30 PM )

Comparing DateTime?

I've been doing a good amount of research with this, and used a few codes to get to know how to make this work, but nothing has worked the way I wanted it to, or hasn't worked at all.
The code is:
<?php
$time1 = $user['last_active'];
$time2 = "+5 minutes";
if (strtotime($time1) > strtotime($time2)) {
echo "Online!";
}else{
echo "Offline!";
}
?>
It is supposed to compare the two variables, and find out if the last active variable is greater or less than 5 minutes, and if it is greater, appear offline. I do not know what's wrong as the NOW() updates on each page and stops if the user is not logged in. Any suggestions or help? Thanks.
The $time1 variable is coming from a fetched array that gets the ['last_active'] information that updates on each page.
I fixed my code, but it still doesn't work right, however, I think I have managed to get further than I was..
<?php
$first = new DateTime();
$second = new DateTime($user['last_active']);
$diff = $first->diff( $second );
$diff->format( '%H:%I:%S' );
if($diff->format( '%H:%I:%S' ) > (strtotime("5 minutes"))){
echo "Offline";
}else{
echo "Online";
}
?>
What can I do at this point?
Nobody pointed out that you actually have a bug. The "current time" will never be greater than "the current time +5 minutes"
Your first code sample will work right if you instead use "-5 minutes" as the "online threshold."
Also, comparing a timestamp without date to the output of strtotime() as you do in the second code is not a proper comparison. It has two problems:
Each time a new day comes around, the same time value will be repeated.
The output of strtotime is an integer representing seconds-since-epoch; the output of format() is a textual representation of hours:minutes:seconds within the current date.
As for your question how to calculate time between 2 dates / time, please view the solution on the following posts, that should give you enough information! (duplicate ? )
Calculate elapsed time in php
And here
How to get time difference in minutes in PHP
EDIT AS YOU PLEASE
<?
$first = new DateTime(); // this would hold your [last active]
//$first->modify("-6 minutes");
$second = new DateTime("NOW");
$difference = $second->diff( $first ); // second diff first
if ($difference->format('%i') > 5) { // comparing minutes only in example ( %i )
echo "The user is AFK";
} else {
echo "user might still be active";
}
?>

Looping through dates until a free one is found

I have a function which checks my database to see if a date exists, if it does exist, i want to display the next date which isnt in the database.
Is this possible?
My function returns 1 if there is a date in the database and 0 if there isnt, im using codeigniter, but not using any built in functions.
Its basically an availability checker, it allows us to input many different dates in the database, so calling my function i use
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y'));
The i use a if statement to check if the first time it runs it returns a value, this is how i have it
if($availcheck > 0){
// loop through the next dates and run the function again to see if it returns 0
} else {
echo 'available now';
}
I guess i would add 1 to the current date, check that one, then add another 1 and check that and so on.
Im just not sure how.
Cheers,
if i understand you correct , your problem is adding the day ?
if so i would suggest using the epoch or unix time
so convert the date to unix time using mktime than just add 1 day in seconds (24*60*60)
and then convert back to d/m/y format.
you can use the date function.
$date = time(); // get current timestamp
while ($availcheck) // while date IS found in database
{
$availcheck = $ci->availability->check_availability_by_date(date('d/m/Y',$date));
$date = $date + (24*60*60); // add one day
}
$date = $date - (24*60*60); // reduce one day
echo date('d/m/Y',$date); // prints the first date that is not in the DB
This SQL code could work for me.
$today = date("Y-m-d"); //today
$sql = "SELECT date FROM calendar WHERE date>'{$today}' AND date<='2100-12-31' AND date='0000-00-00' LIMIT 1";
Since you can't determine the ending date, 2100 could be for testing.

Categories