0

Assume that i have two variables in php:

$year
$month

Then I want to make another variable:

$date

which:

$date=$year-$month-25

So, if I have 2012 in $year and 7 for $month, $date will be 2012-07-25.

Actually, I will compare it with some date in MySQL. $year and $month are inputted by user.

anybody have a solution? The solution either how to make $date or anything as long it can be comparred with a date in mysql. Thanks before. ^^

7 Answers7

2

You can make a unix timestamp through this:

$myDate = mktime(0, 0, 0, $month, 25, $year);

This is a pretty useful thing to have, as you can format it into all sorts of nice via:

echo date("Y-m-d", $myDate);
// Prints something like: 2012-07-25

or

echo date("l", $myDate);
// Prints something like: Monday

or

date('l jS \of F Y h:i:s A', $myDate);
// Prints something like: Monday 8th of August 2005 03:12:46 PM
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
1

You can do as follows

$complete_date = $year."-".$month."-25";

which gives you 2012-7-25

Vamsi
  • 873
  • 1
  • 8
  • 16
  • 1
    These dates are not easily comparable to MySQL dates in PHP, you need to convert to a timestamp first so integer comparison (which is reliable can occur) – Sammaye Jul 31 '12 at 09:25
  • thanks. can comparison between $complete_date and a date type in mysql give a correct ressult? ^^ – user1545296 Jul 31 '12 at 09:32
1

Please, read the "php manual" for concat your PHP string.

it's not

$date = $year-$month-25;

it is

$date = $year . '-' . $month . '- 25';

or

$date = $year . "-" . $month . "- 25";

but simple quote is more optimize for php string.

Doc Roms
  • 3,288
  • 1
  • 20
  • 37
  • you want just compare two date? ahhh, OKay, you want to convert all date in Timestamp, after, you can find difference between your two Timestamp (read [this](http://stackoverflow.com/questions/113829/how-to-convert-date-to-timestamp-in-php) for more details in timestamp) and convert difference in days, or month, or all what you want ^^ – Doc Roms Jul 31 '12 at 09:34
1

The solution either how to make $date or anything as long it can be comparred with a date in mysql

The key here is use of strtotime to create and compare.

MySQL dates can be converted to integer through the use of strototime:

strtotime($mysql_date);

Then you can get time() and compare to two:

time()<>strtotime($mysql_date) // then the two dates are not equal.
Sammaye
  • 43,242
  • 7
  • 104
  • 146
1

You can use mktime function

$date = date('Y-m-d',mktime(0,0,0,$month,25,$year));
Dr. Dan
  • 2,288
  • 16
  • 19
1

Well, I would use mktime to get the timestamp of the date ( http://php.net/manual/de/function.mktime.php ) and use the command unix_timestamp(date(yourfield)) in mysql to compare them. (the date() withing unix_timestamp is only required when you save datetime values and not pure date values)

Najzero
  • 3,164
  • 18
  • 18
1

Since mysql dates are usually in this format Y-m-d by default, you can use

$thedate = date('Y-m-d',mktime(0,0,0,$month,25,$year));
where $month and $year are based on the user input. Of course you have to make the user input it in the format you want by using select/lists.
Yanki Twizzy
  • 7,771
  • 8
  • 41
  • 68