Jump to content

Subtracting a date format...


andyhajime

Recommended Posts

Hey guys, hope someone can help me out on this problem.

 

 

I'm actually doing a patch fixed for a LIVE website.

When someone add a new entry, a date format (below) was stored in the mysql database, but the catch is, it's stored based on the expiry date for that entry.

 

$Expired_On = date('YmdHi', strtotime('+30 days'));
// results: 201002161133

 

I wish I can add new column on the table so it can store the actual posting date. I cannot do much cause this is a LIVE site and the entries are huge. So I figures I need to subtract the date after querying it out and minus off 30 days from it.

eg: "201002161133" -30 days

 

But I not sure how to do it.

Any suggestion guys?

Link to comment
Share on other sites

The example date you show, looks like a unixtimestamp which is stored as an integer, and is the number of seconds since 01/01/1970.

 

so if that figure you are showing is a date 30 days in the future, then subracting 30days worth of seconds from it will subtract 30 days.

 

Either way, the php function time() will give you the timestamp for the time it is run. ie the number of seconds since 01/01/1970.

 

You can store that in an int field in your db, and use the date() or strftime() functions to display it.

 

Other than that, what is it, you specifically want to know

Link to comment
Share on other sites

You can store that in an int field in your db, and use the date() or strftime() functions to display it.

 

Bro, appreciate your help but in the early stage of the post, I've already stated that I cannot edit the sql database. It's a running website with hundreds of entries everyday. Unless I want to get myself sued, then I can follow your method. The only way I can pull this off is to minus off the 30days from 201002161133 on that very spot. I was given a green light to edit only one file... nothing else.  :-\

 

 

I came up with this few hours ago.

 

$PROPERTY = "201002161133";
$day = substr($PROPERTY, 6,-4);
// 16
$month = substr($PROPERTY, 4,-6);
// 02
$year = substr($PROPERTY, 0,-;
// 2010

 

Now, how do I go about from here is the 6 dollar question.

Link to comment
Share on other sites

Oh sh*t... sorry bro, don't think this will work. I just discovered something in the website admin panel.

There's a setting field for changing the number of days.

 

based from your codes and the earlier ones I've posted, it dont work.  :shrug:

 

 

$PROPERTY = "201002161133";
//$thirty = 30 * 24 * 60 * 60;
$propminus30 = strftime("%Y%m%d",strtotime($PROPERTY) - ' . $DAY_OF_EXPIRED() . ' days);
echo $propminus30;

Link to comment
Share on other sites

Ops sorry typo... my bad  ;D

 

function DAY_OF_EXPIRED()
{
$FIND  = mysql_query("SELECT * FROM setting WHERE title = 'expire date'");
$FOUND = mysql_fetch_array($FIND);
return $FOUND['value'];
}

 

 

The first code I show at the beginning is

$Expired_On = date('YmdHi', strtotime('+30 days'));
// results: 201002161133

 

In which if i combine the function would be

$Expired_On = date('YmdHi', strtotime('+'.DAY_OF_EXPIRED().' days'));
// results: 201002161133

 

Is it possible using the same method as above for your codes.

Link to comment
Share on other sites

Hey guys, hope someone can help me out on this problem.

 

 

I'm actually doing a patch fixed for a LIVE website.

When someone add a new entry, a date format (below) was stored in the mysql database, but the catch is, it's stored based on the expiry date for that entry.

 

$Expired_On = date('YmdHi', strtotime('+30 days'));
// results: 201002161133

 

I wish I can add new column on the table so it can store the actual posting date. I cannot do much cause this is a LIVE site and the entries are huge. So I figures I need to subtract the date after querying it out and minus off 30 days from it.

eg: "201002161133" -30 days

 

But I not sure how to do it.

Any suggestion guys?

 

Hey andy,

Please note that PHP5 has a great library for date and time http://www.php.net/manual/en/book.datetime.php

A short example will be:

 

$date = new DateTime("18-July-2008 16:30:30");

echo $date->format("d-m-Y H:i:s").'<br />';

 

 

Link to comment
Share on other sites

Hey andy,

Please note that PHP5 has a great library for date and time http://www.php.net/manual/en/book.datetime.php

A short example will be:

 

$date = new DateTime("18-July-2008 16:30:30");

echo $date->format("d-m-Y H:i:s").'<br />';

 

I learn from trials of errors and people's coding. I'm more to practical person. not theory. but thanks anyway.

Link to comment
Share on other sites

You can use date() where I use strftime()

 

They are very similar functions but do have a different set of format specifiers and way of setting those specifiers.

 

I use strftime() as I am historically a C programmer, but date() does have a few extra useful specifiers.

Link to comment
Share on other sites

I manage to solve it by my own finally... and thanks to Tizag for the example.  :wtf:

I just don't get it why everyone is showing the tougher complex method when there's a shortcut to it.  ;D

Anyway... sharing the coding if anyone interested.

 

function DAY_OF_EXPIRED()
{
$FIND  = mysql_query("SELECT * FROM setting WHERE title = 'days to expire'");
$FOUND = mysql_fetch_array($FIND);
return $FOUND['value'];
// 30
}

$Expired_On = date('YmdHi', strtotime('+'.DAY_OF_EXPIRED().' days'));
// 201002161133

$day = substr($Expired_On, 6,-4);
// 16
$month = substr($Expired_On, 4,-6);
// 02
$year = substr($Expired_On, 0,-;
// 2010

$Before_Expired= mktime(0, 0, 0, $month, $day-'.DAY_OF_EXPIRED().', $year);
echo date("d m y", $Before_Expired);

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.