• Home
  • Blog
  • Demos
  • About
  • ------------------
  • Articles
  • Csharp(59)
    • controls (4)
    • methods (5)
    • tutorials (50)
  • Html(1)
    • tutorials (1)
  • Java(10)
    • mobile (10)
  • Javascript(5)
    • tutorials (5)
  • Linux(3)
    • tutorial (3)
  • Math(10)
    • tutorials (10)
  • Php(15)
    • functions (1)
    • mysql (6)
    • tutorials (8)
  • Sql(23)
    • tutorials (23)
  • Vba(1)
    • tutorials (1)

Add time to timestamp

Written: 2009-09-15 23:50:20
Mood: Nulled
Subject: programming
Sometimes you get a timestamp from a database, most likely MySql =D and you realize that you have to alter the time for some reason.

This is easily fixed with a small function, like this:

<?php

function add_date($_timestamp = '', $add_hours = 0, $add_minutes = 0, $add_seconds = 0, $add_years = 0, $add_months = 0, $add_days = 0){
if($_timestamp){
$_split_datehour = explode(' ',$_timestamp);
$_split_data = explode("-", $_split_datehour[0]);
$_split_hour = explode(":", $_split_datehour[1]);

return mktime ($_split_hour[0] + $add_hours,
$_split_hour[1] + $add_minutes,
$_split_hour[2] + $add_seconds,
$_split_data[1] + $add_years,
$_split_data[2] + $add_months,
$_split_data[0] + $add_days);
}
}

?>


Now, just call it with your timestamp:

$result = mysql_query("SELECT * FROM userList");
while($rows=mysql_fetch_array($result)){
echo "Username: ".$rows['username'];
echo "<br />";
echo "Password: ".$rows['password'];
echo "<br />";
echo "Register date: ".add_date($rows['r_date'], 8); //column r_date being a TimeStamp
echo "<br /><br />";
}


The above code will output the date from the r_date column with 8 hours added to it.

It's one way, bet there are many =)