php_novice2007 Posted August 28, 2007 Share Posted August 28, 2007 Hi, So now I've got a string in the form $s = "yyyy-mm-dd hh:mm:ss" and a positive integer (might be 0) representing a number of days $d = 5. I want to create a new string $s1 that is $s- $d eg $s = "2007-09-01 12:24:22", $d = 5 ==> $s1 = "2007-08-27 12:24:22" I'm thinking of something like $s1 = date(strtotime($s)) - date(strtotime($dd)); but I have no idea how to represent 5 days into yyyy-mm-dd hh:mm:ss format, it doesnt actually make sense to convert a day to that form... How should I be doing this? Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/ Share on other sites More sharing options...
btherl Posted August 28, 2007 Share Posted August 28, 2007 mktime()[/code] is often used for date arithmetic. Examples are in the link. Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/#findComment-335968 Share on other sites More sharing options...
chronister Posted August 28, 2007 Share Posted August 28, 2007 Me personally I find it easier to just keep all dates in unix timestamp. You don't have to worry about converting them, you can perform mathematical equations on them and one string gives you seconds, minutes, hours, day, month & year. Do yourself a favor, just keep them all in unix timestamp format in the db and in the script. Then when you need to display in a certain format, just use date(). When I first started playing with MySql, I thought "hey let's keep dates in the *preferred* mysql format", I quickly realized that its a pain in the ass to try and manipulate that date string. If your just storing and displaying, then ok no prob... but as you can see the moment you need to do math with time you need a unix timestamp. So cut the middle man and just store them in that format to begin with and you'll save some headache. Nate Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/#findComment-335981 Share on other sites More sharing options...
php_novice2007 Posted August 28, 2007 Author Share Posted August 28, 2007 hm true, but the database I'm given to work with has the timestamp like that, and I'm almost at the end of my project so its too late to change it now and have to redo sooo many pages... I'll keep it in mind for next time Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/#findComment-336086 Share on other sites More sharing options...
php_novice2007 Posted August 28, 2007 Author Share Posted August 28, 2007 Hi, Sorry I'm still stuck on this, mktime require a date.. I don't know how to have a generic equation that works out the date.. Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/#findComment-336160 Share on other sites More sharing options...
chronister Posted August 28, 2007 Share Posted August 28, 2007 Are ya looking for something like this? outputs 2007-08-27 12:24:22. mktime() is mainly used for making a unix timestamp that is in the future or past. If you wanted to get the day of the week that May 4th fell on in 1985, mktime() could calculate that for ya. <?php $seconds_in_a_day=86400; // the num of seconds in a day $s = "2007-09-01 12:24:22"; //time string $d = 5; // days to factor in $dd=$d*$seconds_in_a_day; get the number of seconds by multiplying days, by seconds in a day $s1 = strtotime($s)-$dd; //perform our subtraction echo date("Y-m-d h:i:s",$s1); // echo the date in the format we want. ?> The key here is strtotime(). That will take your date string and turn it into a unix timestamp and then you can do your math with it. Then you just have to convert it back to the format you need. Nate Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/#findComment-336212 Share on other sites More sharing options...
AndyB Posted August 28, 2007 Share Posted August 28, 2007 Or: <?php $date1 = "2007-09-01 12:24:22"; // example $num_days = 5; // example $timestamp = strtotime($date1); $newtime = $timestamp - 86400 * $num_days; $newdate = date("Y-m-d h:i:s", $newtime); echo $date1. " minus ". $num_days. " days is ". $newdate; ?> Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/#findComment-336221 Share on other sites More sharing options...
php_novice2007 Posted August 28, 2007 Author Share Posted August 28, 2007 Thanks all! I figured out a third way: $s = "2007-09-01 12:24:22"; list($year,$month,$day,$hour,$min,$sec)=split('[- :]',$s); $d = 5; $subDay = $day - $d; $s1 = date("Y-m-d H:i:s", mktime($hour,$min,$sec,$month,$subDay,$year)); Seems to work Quote Link to comment https://forums.phpfreaks.com/topic/66999-another-date-question/#findComment-336584 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.