PHP Remove Content when date hits - php

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
}

Related

PHP date before a specified time

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)
}

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
}
?>

Compare two database entries and produce span class

I wonder if anyone could help me with this.
I have two fields in a database that are shown using:
1-<?php echo JHTML::_('date', $row->created_date, 'd/m/Y'); ?>
2-<?php echo $row->delivery_date; ?>
The first one, is written to the database as a full date with time, and as you can see above, I strip the output to only show the date. In the database it would appear as '2013-09-10 11:56:52'
The second is from a text entry form field, that has just text saved to the database in the format d/m/Y. In the database this appears as '19/09/2013'
Is there a way I can produce an if statement, that will add a span and class tag around the 2nd line if this condition is true:
"If the $row->delivery_date is within 21 days after (and including)
$row->created_date."
Would it ultimately be best if I made them both full date values? Would that make it easier to calculate how many days apart they are?
Convert Dates to a common format using strtotime() (e.g. PHP works nicely with unix timestamp's):
$created_date = strtotime($row->created_date); // These are basically seconds
$deliver_date = strtotime($row->delivery_date);
Calculate the difference between the two and covert it to days (each day is 86400 seconds):
$days = ceil(abs($deliver_date - $created_date) / 86400);
Check $days in your if() statement for echoing your span.
Something like:
if($days <= 21)
{
echo '<span class="delivery_warning">' . $row->delivery_date . '</span>';
}
else
{
else { echo $row->delivery_date; };
}

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.

Check if a given date is past

I have a week calendar that holds events, and want that users can't add events for the past days. So I'm tring to use a function like that:
if( strtotime($this->day) < time() ){ // date format is YYYY-MM-DD
// date is past
}else{
// date is not past
}
It seems to works fine, except that it consider today date as a past day. What am i doing wrong?
Simpler ->
if(strtotime($this->day) < strtotime(date('Y-m-d')))
{
...
}
else
{
...
}
A timestamp never contains only the date, but is always down to the current second. strtotime($this->day) is going to return today's date at 0:00, while you are comparing it against now, say, 11:12.
You could use strtotime("$this->day 12:59:59pm"); (if the format of $this->day allows for that) or use tomorrow's timestamp.
if(strtotime($this->day) < mktime(0, 0, 0)){
// date is past
} else {
// date is not past
}

Categories