I am trying to get upcoming birthdays from a database so I can create a mailing list for those customers. $pet_bdate - The admin supplies a date with JQuery datepicker in the format dateFormat: 'yy-mm-dd'. $plus_days - He can also choose For Dates Range - Plus Days from Chosen Date from 0 to 14.
Server config: Php v 5.2.17 MySQL v 5.5.23 (So several more advanced Php functions don't work as well as MySql)
The column petbday is of type DATE .
Here is the query:
SELECT * FROM `customer` c LEFT JOIN `pet` p ON (c.customer_id = p.customer_id)
WHERE (DATE_FORMAT(p.petbday, '%m-%d')
BETWEEN DATE_FORMAT(2014-04-13, '%m-%d') AND DATE_FORMAT(DATE_ADD(2014-04-13, INTERVAL 7 DAY), '%m-%d'))
GROUP BY c.customer_id ORDER BY firstname, lastname, email
And here is the Php code that makes it:
SELECT * FROM `" . DB_PREFIX . "customer` c LEFT JOIN `" . DB_PREFIX . "pet` p ON (c.customer_id = p.customer_id)
WHERE (DATE_FORMAT(p.petbday, '%m-%d') BETWEEN DATE_FORMAT(" . $pet_bdate . ", '%m-%d') AND DATE_FORMAT(DATE_ADD(" . $pet_bdate . ", INTERVAL " . $plus_days . " DAY), '%m-%d')) GROUP BY c.customer_id ORDER BY firstname, lastname, email
This query returns ZERO rows for a date that has birthdays in the DB.
How can I hand a formatted date to query so to ignore year?
I tried formating it prior to handing it to query but when it comes to calculating dates with Php I got a little mixed up.
Any suggestions on how to resolve this?
Try using DAYOFYEAR instead of string comparison.
Update: I also need to add that your dates should be wrapped in colons to work properly '2004-04-15'.
This is my solution:
SELECT * FROM `customer` c LEFT JOIN `" . DB_PREFIX . "pet` p
ON (c.customer_id = p.customer_id)
WHERE (DAYOFYEAR(p.petbday) BETWEEN (DAYOFYEAR('" . $pet_bdate . "'))
AND (DAYOFYEAR(DATE_ADD('" . $pet_bdate . "', INTERVAL " . $plus_days . " DAY))))
GROUP BY c.customer_id ORDER BY firstname, lastname, email
So in order to get DATE RANGE I used DAYOFYEAR (as andig suggested) on the column itself p.petbday and on both range dates:
$pet_bdate - ORIGINAL STARTING DATE
DATE_ADD('" . $pet_bdate . "', INTERVAL " . $plus_days . " DAY) - ORIGINAL
STARTING DATE PLUS INTERVAL DAYS
I used DATE_ADD because my MySQL version is v5.5.23
NOTE: Both dates need to be passed as string (hence the single quotes).
Hope you found this useful...
$('input[name="pet_bdate"]').datepicker({
dateFormat: 'yy-mm-dd',
yearRange: '2000:2000', // one year - leap year
changeMonth: true,
changeYear: false, // NO **Year** drop down box
numberOfMonths: 1,
minDate: new Date('2000/01/01'), // START date
maxDate: new Date('2000/12/31'), // END date
beforeShow: function(input, instance) {
var firstDate = new Date('2000/01/01');
if (this.val != 0) {
$('input[name="pet_bdate"]').datepicker( 'setDate' , $('input[name="pet_bdate"]').val());
} else {
$('input[name="pet_bdate"]"]').datepicker('setDate', firstDate);
}
}
});
$('#ui-datepicker-div').removeClass('ui-helper-hidden-accessible');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.