1

The system is for phone contract comparison. Originally we could filter the result by length, given in months. However we've opted to storing this as a timestamp of the end date of contract in the database.

The original PHP was as follows:

$min = min($_POST["length"])-6;
$max = max($_POST["length"]);
$query[] = "length BETWEEN $min AND $max";

In summary, here's what we have at the moment:

  1. In the database is stored the end-date of the contract (as a timestamp)

  2. The post value contains how many months from now it should last (eg. 7-12 months would have a value of 7)

How can we change the PHP calculations to get the between values from the database?

Many thanks, Nick.

1 Answer 1

3
<?php
    $diff = 7;     // This may be set to $_POST['length']
    $max = date("Y-m-d");
    $min = date("Y-m-d", strtotime("$diff months ago"));
    $query[] = "DATE(`timestamp_field`) BETWEEN CAST($min AS DATE) AND CAST($max AS DATE)";
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Is it not essential to cast the $min and $max dates to ensure the correct data type? DATE(timestamp_field) BETWEEN CAST($min AS DATE) AND CAST($max AS DATE)
@Daniel I'm unsure of the CAST function- what does it do?
@Daniel Ah, yes. I normally keep my date and time type columns separate. So, this was prebuilt code. :)
@NickPrice; The CAST function makes sure that the value is of a certain data type. If you would compare $min $max without casting, the values would probably be treated as string, and not date. See link: dev.mysql.com/doc/refman/5.0/en/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.