Set timezone in PHP and MySQL

In PHP: <?php define(‘TIMEZONE’, ‘Europe/Paris’); date_default_timezone_set(TIMEZONE); For MySQL: <?php $now = new DateTime(); $mins = $now->getOffset() / 60; $sgn = ($mins < 0 ? -1 : 1); $mins = abs($mins); $hrs = floor($mins / 60); $mins -= $hrs * 60; $offset = sprintf(‘%+d:%02d’, $hrs*$sgn, $mins); //Your DB Connection – sample $db = new PDO(‘mysql:host=localhost;dbname=test’, ‘dbuser’, … Read more

Timezones in SQL DATE vs java.sql.Date

The JDBC specification does not define any details with regards to time zone. Nonetheless, most of us know the pains of having to deal with JDBC time zone discrepencies; just look at all the StackOverflow questions! Ultimately, the handling of time zone for date/time database types boils down to the database server, the JDBC driver … Read more

Why does Java’s Date.getYear() return 111 instead of 2011?

Those methods have been deprecated. Instead, use the Calendar class. import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public final class DateParseDemo { public static void main(String[] args){ final DateFormat df = new SimpleDateFormat(“MM/dd/yyyy”); final Calendar c = Calendar.getInstance(); try { c.setTime(df.parse(“04/12/2011”)); System.out.println(“Year = ” + c.get(Calendar.YEAR)); System.out.println(“Month = ” + (c.get(Calendar.MONTH))); System.out.println(“Day = ” … Read more

sqlite select with condition on date

Some thing like this could be used: select dateofbirth from customer Where DateofBirth BETWEEN date(‘1004-01-01’) AND date(‘1980-12-31’); select dateofbirth from customer where date(dateofbirth)>date(‘1980-12-01’); select * from customer where date(dateofbirth) < date(‘now’,’-30 years’); If you are using Sqlite V3.7.12 or greater Dont use date(dateofbirth) just use dateofbirth. So your query would look like this: select * … Read more

Javascript Date: next month

You’ll probably find you’re setting the date to Feb 31, 2009 (if today is Jan 31) and Javascript automagically rolls that into the early part of March. Check the day of the month, I’d expect it to be 1, 2 or 3. If it’s not the same as before you added a month, roll back … Read more