benphp Posted October 12, 2009 Share Posted October 12, 2009 This will be easy for one of you gurus. I want to fetch the date from a variable date, for example: <?php $mydate = ""; $daysaway = 400; $startdate = "5/5/2005"; //I need how to get the date that was 400 days ago print "$mydate was $daysaway days from $startdate."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/177469-solved-how-to-calculate-days-from-variable-date/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 12, 2009 Share Posted October 12, 2009 Assuming your start date is mm/dd/YYYY - <?php $daysaway = 400; $startdate = "5/5/2005"; //I need how to get the date that was 400 days ago $mydate = date('m/d/Y',strtotime("$startdate - $daysaway days")); print "$mydate was $daysaway days from $startdate."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/177469-solved-how-to-calculate-days-from-variable-date/#findComment-935734 Share on other sites More sharing options...
benphp Posted October 12, 2009 Author Share Posted October 12, 2009 Beautiful - thanks! Quote Link to comment https://forums.phpfreaks.com/topic/177469-solved-how-to-calculate-days-from-variable-date/#findComment-935736 Share on other sites More sharing options...
gizmola Posted October 12, 2009 Share Posted October 12, 2009 Your question is beyond unclear. "I want to fetch the date from a variable date" could be interpreted a bunch of different ways. Your example code doesn't help. What are you attempting to derive? $startdate - $daysaway = Answer is a Date. or $mydate = $startdate? = Answer is number of days difference Quote Link to comment https://forums.phpfreaks.com/topic/177469-solved-how-to-calculate-days-from-variable-date/#findComment-935738 Share on other sites More sharing options...
benphp Posted October 12, 2009 Author Share Posted October 12, 2009 It was perfectly clear to PFMaBiSmAd. Thanks again PFMaBiSmAd! Quote Link to comment https://forums.phpfreaks.com/topic/177469-solved-how-to-calculate-days-from-variable-date/#findComment-935753 Share on other sites More sharing options...
gizmola Posted October 12, 2009 Share Posted October 12, 2009 No it wasn't clear at all. PFMaBiSmAd just made an assumption, and guessed right. Sometimes that happens, so you got lucky, nothing more. In the future try and work a bit harder when you're formulating your questions. Quote Link to comment https://forums.phpfreaks.com/topic/177469-solved-how-to-calculate-days-from-variable-date/#findComment-935765 Share on other sites More sharing options...
tomdchi Posted October 13, 2009 Share Posted October 13, 2009 I use mysql to get a lot of my dates. example: <?php require ('dbcon.php'); $startdate = date('Y-m-d'); $days = 400; $query = "SELECT DATE_SUB( '$startdate', INTERVAL $days DAY )"; $newdate = $dbLayer->FetchRow($query); echo $newdate; ?> For adding: <?php require ('dbcon.php'); $startdate = date('Y-m-d'); $days = 400; $query = "SELECT DATE_ADD( '$startdate', INTERVAL $days DAY )"; $newdate = $dbLayer->FetchRow($query); echo $newdate; ?> I use a custom mysqli class from http://www.robpoyntz.com/blog/?p=189 for the db connection. Quote Link to comment https://forums.phpfreaks.com/topic/177469-solved-how-to-calculate-days-from-variable-date/#findComment-935863 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.