Archive

Archive for the ‘mysql’ Category

php date add – date math compounded

November 15, 2011 Leave a comment

php
This will probably be an elementary post for most, but recently, I was working on a problem that required determining the expiration of a membership and to write some rules about auto-renewing that membership 10 days prior to expiration. Instead of writing any kind of painful date math, or having to rely on mysql for dateadd functions, strtotime accomplished what I needed:


$now = time();
//expire the membership in a year
$MembershipExpiration = date('Y-m-d', strtotime('+1 year', $now));
//renew the membership 10 days prior to expiration:
$MembershipRenewal = date('Y-m-d', strtotime('+1 year -10 day', $now));

I just threw in the “-10 day” to see what would happen, and those smart cookies who built php already have it all under control.

Categories: mysql, php

mysql order by in clause

August 8, 2011 Leave a comment

Need to order a query by the results pulled according to the order in the “in” clause:

SELECT * FROM tbl_name WHERE column_a IN ( val1, val2, val3 )
ORDER BY FIELD(column_a, val1, val2, val3);

Categories: mysql, php