More on Dates in PHP

I ran into quite a few blips when I tried to post some dates to the MySQL database. Very often,  I got all zeros input into the database. Then, it showed the wrong dates (1969 or something). The following code is what I arrived at after a lot a research, a call to my Mom for more research, and a bit of testing. I’m thrilled to finally have this working!

To start off with, your date must be in YYYY-MM-DD format. For me, that was simple. Just change the initial session variable.

 $_SESSION['tdate'] = Date("Y-m-d");
 //or for testing
 $_SESSION['tdate'] = "2012-09-12";
 $fv = array();

 //$fv (form values) array filled from $_POST data and validated //then //execute INSERT INTO with date
 $insert_vars = "Fname, Lname, `date-regis`";
 $insert_vals = "'" . $fv['fname'] . "', '" . $fv['lname'] . 
 ", '" . $fv['regdate'] . "'";

 $isql = "INSERT INTO " . $mydatabase .
 "(" . $insert_vars . ") " .
 "VALUES (" . $insert_vals . ")";

 $iresult = mysql_query($isql, $con);

 if(! $iresult )
    die('Could not enter data: ' . mysql_error());

 //lastly, I wanted to display the date a bit differently
 $datetodisplay = date("m/d/Y", strtotime($row['date-regis']));

A few things to note about the above code.

  1. I’ve learned to never use hyphens in MySQL column names a little too late. Thankfully, the back tick (`) helps keep me from having to reinvent the wheel (see the column `date-regis`).
  2. The date in YYYY-MM-DD format is insert into the database enclosed in single quotes (‘) just like a string.
  3. The last line of code there can be used to initialize your dates into the proper format for the databases, if need be.

As the title suggests, I’ve written on dates in PHP before. That post is all about comparing dates.

Redirecting in PHP

I mistakenly thought redirecting the user would be a simple and well documented process in php. NOT! I’ll get right to what actually works.

function redirect($url) {
 if (strlen($url) > 0) {
 if (headers_sent()) {
 echo "<script>document.location.href='" . $url . "';</script>";
 echo "<noscript><meta http-equiv="refresh" content="0;'" . $url . "'" /></noscript>";
 else {
 header("Location: " . $url);

The above block of code is placed in the header, and it contains 3 methods of redirection. I doubt the php “header()” option will work at all, but I left it in for completeness. header() has to run before ANYTHING else on the page does, and no matter how I put that in, it did not work in a situation where I needed to pass it $url.

The preferred method is to use that tiny bit of JavaScript, and I put META refresh in there as a backup for the odd situation when JavaScript is disabled.

Credit where credit is due:

Comparing Dates, Learning PHP

I’m learning a lot of PHP here recently. I’ve never programmed in it before, but it is a fun little language. The nuances of the syntax are interesting sometimes – very typo prone! I’ve found several sites invaluable in getting the hang of the syntax, but I only recently thought to put them in here. If I find more, I’ll add them in later!

#1: I’ve referenced w3schools a lot over the years for a lot of different langues. It is a fantastic site for basic reference.

The useful site that spawned this post is actually where I found a simple method for comparing dates utilizing the strtotime function. I love how he did it “the hard way” first. That is totally how I was planning on doing it, and why I was searching for an easier way!

Reading the post is worthwhile, but sometimes I just want to skip to the code. The following is a sample of the code I wrote based on Mike D’Agostino’s helpful writing.

$regopen = 0;
//convert dates to comparable formats
$date1 = strtotime($startdate);
$date2 = strtotime($earlydate);
$date3 = strtotime($enddate);
$today = strtotime($todaydate);
//now to compare - it's so easy its beautiful
If (($today >= $date1) AND ($today <= $date3)) {
$regopen = 1;
   //early pricing or regular?
$cost = $costreg;
If ($today <= $date2) $cost = $costearly;