Jump to content

datetime


acidglitter

Recommended Posts

So I have a certain date in a table (and the type of field is datetime), what code would show what that date would be in 7 days?

 

 

I'm having each row expire after 7 days, and I want to be able to make a countdown of how many days/hours/minutes are left until it expires.

Link to comment
Share on other sites

$one_week_from_today=mktime(0,0,0,date('m'),date('d')+7,date('y'));

 

This is the jist of it, it assumes that you want the date not the time. This will give you 12:00:00am 7 days from now,

 

for more help check out the date() function and mktime() function. Working with time and dates in PHP can be confusing at first, but once you get the hang of working with the seconds since epoch format, it is really easy to get dates in the past, present, and future. You may want to look at strtotime() as well. This will convert the datetime format in the db to a seconds since epoch format for ya.

 

Enjoy

Link to comment
Share on other sites

Thanks for the replies :)

 

I get lost trying to do 7 days after the started date.. this is what I have and it doesnt work...

$week=$row['started']+mktime(0,0,0,date('m'),date('d')+7,date('y'));

 

Maybe I'm supposed to change the $row['started'] into the date() format first?

Link to comment
Share on other sites

Try this,

 

<?php
$week=strtotime($row['started'])+mktime(0,0,0,date('m'),date('d')+7,date('y'));

echo date('h:i:s m/d/Y', $week );

?>

 

This should give you something like 3:23:47 8/8/2007 as long as I got the date() formatting right.

 

the datetime field format is something like 1:23:56 5-4-2007 ..... I think

 

In order to use this date/time in an equation you have to convert it to the proper time string, which is where strtotime() comes in.

 

It's late and I am tired so I may have messed this up a little, but I am fairly confident that what I wrote will work.

 

Nate

 

Link to comment
Share on other sites

you could do this in teh query itself (mysql date/time support is better than php's)

<?php
$qry = "SELECT date_add(`datefield`, INTERVAL 1 WEEK) as `nxtweek` FROM `table` WHERE {conditions}...";
$qry = mysql_query($qry);

$qry = mysql_fetch_assoc($qry);
$newdate = $qry['nxtweek'];
?>

 

try that and see what happens...

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.