Solved

PHP if statement based on time

Posted on 2016-08-03
13
46 Views
Last Modified: 2016-09-02
Hi,

I have this code

if ($inputs['start_date'] && $inputs['end_date']")

Open in new window


I need it to work if the start_date is that date or in the future && if end_date is the end date or six weeks from that date, can someone write that for me so \i can learn from it plz

Thanks Simon
0
Comment
Question by:pc-buddy
  • 5
  • 4
  • 2
13 Comments
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 250 total points
ID: 41741429
The simplest way is to use strtotime() to convert a date to a numeric timestamp, like this:

$now = time();
if ($now >= strtotime($inputs["start_date"]))  && ($now <= strtotime($inputs["end_date"]))
{
}

However, strtotime() gives you a 32-bit number, which means it only covers dates between 1970-01-01 and sometime in 2036. If you need dates outside that range, you can use the DateTime objects in PHP.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 41741430
We have articles here that show how to handle Date/Time values in PHP.  Lots of examples!

Procedural:
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html

Object-oriented:
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

The line of code in the original question contains a PHP parse error, so it's not a useful starting point for any programming.  Here are some examples that might be helpful, but since this is a data-dependent question, it would be best for you to post the data and we can show you the code for dealing with the data.
$now = strtotime('now');            // CURRENT DATE / TIME
$old = strtotime('today - 3 days'); // IN THE PAST: THREE DAYS AGO
$new = strtotime('now + 6 weeks');  // IN THE FUTURE: SIX WEEKS FROM THIS MOMENT

Open in new window

0
 

Author Comment

by:pc-buddy
ID: 41741431
Brillaint thanks, i'll give that a go

Simon
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41741456
Jonathan: Check this.  Seems to work on most systems I've used.
https://iconoun.com/demo/temp_gr8gonzo.php
<?php // demo/temp_gr8gonzo.php
/**
 * https://www.experts-exchange.com/questions/28961356/PHP-if-statement-based-on-time.html
 *
 * http://php.net/manual/en/function.strtotime.php
 */
error_reporting(E_ALL);
echo '<pre>';


// PEARL HARBOR DAY
$old = 'December 7, 1941';
$txt = date('c', strtotime($old));
echo PHP_EOL . "$txt $old";

// TWO HUNDRED YEAR ANNIVERSARY
$new = 'December 7, 2141';
$txt = date('c', strtotime($new));
echo PHP_EOL . "$txt $new";

Open in new window

Outputs:
1941-12-07T00:00:00-06:00 December 7, 1941
2141-12-07T00:00:00-06:00 December 7, 2141

Open in new window

Best to all, ~Ray
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41741583
Ray - I don't get exactly the same output on my system (trying on a variety of PHP 5.x builds up to 5.6.11).

1941-12-07T00:00:00+00:00 December 7, 1941
1970-01-01T00:00:00+00:00 December 7, 2141

Apparently PHP uses signed integers, so it'll turn the 1941 date into a negative integer and format it properly. However, on the builds that I tried, which are all 32-bit builds, anything beyond the max 32-bit integer will result in a null value for strtotime().

My guess is that you're on a 64-bit build? Per the strtotime() documentation page on php.net:

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

On the plus side, at least I know that my Hello World app that displays the time will still work 293 billion years from now after I move to 64-bit PHP.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41742273
Yes, I'm pretty sure the 64-bit machines take advantage of all 64-bits here.  I still have a 32-bit desktop, but the servers and all the new machines are 64, so I've locked in that 293 billion year future!  Interestingly, I have not had any software fail because I moved it to a 64-bit computer.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41742586
I don't think it's a question of the machine (I'm running on 64-bit Win 10), but rather whether PHP itself was built as a 64-bit binary.
1
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41743242
Could be.  I've never tried building PHP with other than a match on 32-bit or 64-bit, but I can see how a 32-bit build might not take advantage of the full word size.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41776086
This question was answered immediately.

Complete explanations with many examples are provided in the articles referenced here:
https://www.experts-exchange.com/questions/28961356/PHP-if-statement-based-on-time.html#a41741430

A code example showing how to make such a comparison is here:
https://www.experts-exchange.com/questions/28961356/PHP-if-statement-based-on-time.html#a41741429
0
 

Author Comment

by:pc-buddy
ID: 41782369
Dont think i asked to delete this ?
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41782399
The question got closed as part of an automatic cleanup process because there was no activity after 21 days (see comment 41776085). You can always open up another question if the above answers didn't work for you for some reason (but the contributors on EE can't really know unless you tell us).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Please show me easiest post form. 3 31
php call to a non-object 3 32
Access Object's Property 9 22
Scope of $_SESSION 17 27
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now