andyhajime Posted February 21, 2010 Share Posted February 21, 2010 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? Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 The actual date now, as a uinx timestamp is returned by running time() in the php code. if you want to subtract 30 days from a unix timestamp, you first need to convert the 30 days, to seconds 30 * 24 * 60 * 60 Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 I'm sorry but I'm a little dumb about using anything related with time and date... which is the reason why I come here asking for help. Can u rephrase cause I don't quite understand. Quote Link to comment Share on other sites More sharing options...
Dancodim Posted February 21, 2010 Share Posted February 21, 2010 If You keep the date in a datetime field in the database and assuming it is MySql Db, then use the mysql command for it DATE_SUB. If it is stored like unixtimestamp then u need to do what the jl5501 said. Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 Hm... it's ok. Didn't think I'll get much help here. Literally like I said, I am dumb at anything related to date and time. So if you say unixtimestamp and all that, I don't even know what that means. :'( Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 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 Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 ok do this $PROPERTY = "201002161133"; $thirty = 30 * 24 * 60 * 60; $propminus30 = strftime("%Y%m%d",strtotime($PROPERTY) - $thirty); echo $propminus30; Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 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. $PROPERTY = "201002161133"; //$thirty = 30 * 24 * 60 * 60; $propminus30 = strftime("%Y%m%d",strtotime($PROPERTY) - ' . $DAY_OF_EXPIRED() . ' days); echo $propminus30; Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 what does this mean? $DAY_OF_EXPIRED() do you mean $DAY_OF_EXPIRED or DAY_OF_EXPIRED() Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 Ops sorry typo... my bad 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. Quote Link to comment Share on other sites More sharing options...
perseadrian Posted February 21, 2010 Share Posted February 21, 2010 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 />'; Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 ok sure, cool... Like I said... Didn't think I'll get much help here anyway. But thank for ur time. Quote Link to comment Share on other sites More sharing options...
andyhajime Posted February 21, 2010 Author Share Posted February 21, 2010 I manage to solve it by my own finally... and thanks to Tizag for the example. I just don't get it why everyone is showing the tougher complex method when there's a shortcut to it. 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.