Jump to content

query to change datetime in php !


hassank1

Recommended Posts

<?php

function split_date ($dateTime)
{
$splitTime ['year'] = substr ($dateTime,0,4);
$splitTime ['month'] = substr ($dateTime,5,2);
$splitTime ['day'] = substr ($dateTime,8,2);
$splitTime ['hour'] = substr ($dateTime,11,2);
$splitTime ['minute'] = substr ($dateTime,14,2);
$splitTime ['second'] = substr ($dateTime,17,2);	

return $splitTime;
}

$loop = 0;
$query = "SELECT `date` FROM `table`";
$result = mysql_query ($query);

while ($row = $mysql_fetch_array($result))
{
$loop++;

$dateTime = split_date ($row['date']);

$dateTimeTemp = $dateTime['hour'] + 8;

if ($dateTimeTemp > 24)
{
	$dateTime['day']++;
	$temp = 24 - $dateTime['hour'];
                $dateTime['hour'] = 8 - $temp
}
else
{
	$dateTime['hour'] = $dateTime['hour'] + 8;
}

$dateTimeNew = "$dateTime[year]-$dateTime[month]-$dateTime[day] $dateTime[hour]:$dateTime[minute]:$dateTime[second]";

$query = "UPDATE `table` SET date='$dateTimeNew' WHERE id='$loop'";
        mysql_query ($query);

}

?>

 

This should do the trick. You still need to fill in your table name.. I just put "table" there

 

Edit, sry now it's correct

Link to comment
Share on other sites

I had no idea, this is an alternative I did.

But can you go more simple than that?

 

<?php

function split_date ($dateTime)
{
$splitTime ['year'] = substr ($dateTime,0,4);
$splitTime ['month'] = substr ($dateTime,5,2);
$splitTime ['day'] = substr ($dateTime,8,2);
$splitTime ['hour'] = substr ($dateTime,11,2);
$splitTime ['minute'] = substr ($dateTime,14,2);
$splitTime ['second'] = substr ($dateTime,17,2);	

return $splitTime;
}

$loop = 0;
$query = "SELECT `date` FROM `table`";
$result = mysql_query ($query);

while ($row = $mysql_fetch_array($result))
{
$loop++;

$splitTime= split_date ($row['date']);

$dateTimeNew = mktime ($splitTime['hour']+8, $splitTime['minute'], $splitTime['second'], $splitTime['month'], $splitTime['day'], $splitTime ['year']);

$dateTimeNew = date ('Y-m-d H:i:s', $dateTimeNew);

$query = "UPDATE `table` SET date='$dateTimeNew' WHERE id='$loop'";
        mysql_query ($query);

}
?>

Link to comment
Share on other sites

thanks that exactly what I want .. however I think it need some modification because what if day is 30 then +1 .. I should add +1 to months etc... right ??

 

one more thing plz .. to get the current date I am using

 

$DateNow  = date("Y-m-d H:i:s");      //date @ this moment

 

however how can I get the current date + 8 hours ...???

 

thx

 

revraz -> I've tried ur method it didn't work :S ... can u give me ur query to test it ! maybe I've done something wrong in my query..

Link to comment
Share on other sites

OK ..

 

but now how can I save into a variable the current time + 8 hours .. (I mean this time I don't get the value from a table .. rather I want to use the current time and add 8 hours to it )

 

<-- that's another question .. however it's kind of similiar ..

 

thanks .. :) btw sorry because u r coding in a browser :P

Link to comment
Share on other sites

<?php

// Splits the date
function split_date ($dateTime)
{
$splitTime ['year'] = substr ($dateTime,0,4);
$splitTime ['month'] = substr ($dateTime,5,2);
$splitTime ['day'] = substr ($dateTime,8,2);
$splitTime ['hour'] = substr ($dateTime,11,2);
$splitTime ['minute'] = substr ($dateTime,14,2);
$splitTime ['second'] = substr ($dateTime,17,2);	

return $splitTime;
}

// Set the loopcounter to 0
$loop = 0;

// Get the MySQL data
$query = "SELECT `date` FROM `table`";
$result = mysql_query ($query);

// Loop the rows
while ($row = $mysql_fetch_array($result))
{
// Increment the loop variable every loop
$loop++;

// Get the splitted time array
$splitTime = split_date ($row['date']);

// Make new time (adds the 8 hours)
$dateTimeNew = mktime ($splitTime['hour']+8, $splitTime['minute'], $splitTime['second'], $splitTime['month'], $splitTime['day'], $splitTime['year']);

// Format the timstamp into a MySQL DATETIME format
$dateTimeNew = date ('Y-m-d H:i:s', $dateTimeNew);

// Update the row
$query = "UPDATE `table` SET date='$dateTimeNew' WHERE id='$loop'";
    mysql_query ($query);

}
?>

 

I added some comments, this one should work though

and for the current date that's fairly simple

 

<?php

$dateNew = mktime (date ('H')+8, date ('i'), date ('s'), date ('m'), date ('d'), date ('Y'));
$dateNew = date ('Y-m-d H:i:s', $dateNew) ;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.