It is currently October 21, 2017, 02:12:40 AM




Post new topic Reply to topic  [ 1 post ] 
 Useful date functions in PHP :) 
Author Message
Newcomer

Joined: September 11, 2007, 03:55:14 PM
Posts: 1
Location: USA
Post Useful date functions in PHP :)
Well since this is my first post, I figured what better way to introduce myself to the community than to post on the code section(since that is my field). This code I am posting is something I use to add and subtract dates. Though it not be very complex, it is very useful and saved me many lines of code :). And you will also notice I posted a function to control the running of sql statements to simplify coding. Well here it is, enjoy!
Code:
function run_sql($sql){
  if($_SESSION['see_sql'] == true){
   echo "SQL: $sql</br></br>";
  }
  mysql_connect("localhost", "databasename", "awesomeunhackablepassword");
  mysql_select_db("databasename");
  $result = mysql_query($sql);
  $error = mysql_error();

  // check to see if there was an sql error.  If there is no error, return $result otherwise
  // display error message and send a notification email to all Revolve Admins
  // I just left this part in to show some other useful things you could do with this function
  if ($result !== false) {
   return $result;
  }
  else{
   $subject = "Run_Sql() Error";
   $admin_msg = "Revolve has encountered an error while trying \n";
   $admin_msg .= "to execute SQL against the Revolve database.\n";
   $admin_msg .= "\nSQL Statement: $sql";
   $admin_msg .="\nMysql_error:  $error";
   $user_msg = "Run_Sql() Error";
   email($subject, $admin_msg, $user_msg);
   print_header();
   return false;
  }
}

function date_date_sub($date1, $date2){
  /* Uses MySQL to return an integer representing the difference
  * between the 2 date string parameters (yyyymmdd assumed) */
  $sql = "SELECT DATEDIFF('$date1', '$date2') diff";
  $row = mysql_fetch_array(run_sql($sql)) or die("Can't run sql: $sql. ".mysql_error());
  return (int)($row['diff']);
}

function date_int_add($date, $int){
  /* Uses MySQL to return a date string (yyyymmdd assumed)
  * representing the result of date, integer addition. */
  $sql = "SELECT DATE_FORMAT(ADDDATE('$date', '$int'), '%Y%m%d') new_date";
  $row = mysql_fetch_array(run_sql($sql)) or die("Can't run sql: $sql. ".mysql_error());
  return ($row['new_date']);
}

function date_int_sub($date, $int){
  /* Uses MySQL to return a date string (yyyymmdd)
  * representing the result of date, integer subtraction. */
  $sql = "SELECT DATE_FORMAT(($date - INTERVAL $int DAY), '%Y%m%d') new_date";
  $row = mysql_fetch_array($result = run_sql($sql)) or die("Can't complete date-int  subbtraction. ".mysql_error());
  $new_date = $row['new_date'];
  return $new_date;
}



The run_sql function would basically work like this:

Code:
$sql = "SELECT field
        FROM TableOne
        WHERE field = condition";
$result = run_sql($sql) or die("Could not run sql:  $sql ".mysql_error());
While (row = mysql_fetch_array($result)){
  $field = row['field'];
  echo "The field that was returned was $field";
}



So you can see in a large program, this would save you quite a few lines in the end :). Let me know what you guys think. If you have any questions, fire away.


October 12, 2007, 05:31:50 PM
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.