dreampho Posted October 1, 2012 Share Posted October 1, 2012 Hi all. Okay, so I have a date saved in UNIX format, so the date looks like this: 1346689843 - {start} is just my CMS variable. I am trying to first convert the date to dmY format, then remove 14 days. $date = date('dmY',strtotime("{start}")); $due_date = strtotime('-14 day',strtotime($date)); $due_date = date('dmY',$due_date); echo $date; echo $due_date; The $date variable displays 1/1/1970 which is the first day of the UNIX I think. $due_date displays 18/12/1969 So the minus 14 days is working, just not the formatting for $date. How can I get the $date variable to format the date correctly? Am I making some stupid error here? Quote Link to comment https://forums.phpfreaks.com/topic/268971-taking-away-from-a-date/ Share on other sites More sharing options...
ManiacDan Posted October 1, 2012 Share Posted October 1, 2012 The $date variable displays 1/1/1970 which is the first day of the UNIX I think.You're trying to get the date of the string '{start}'. That's not a date. You can't just assume CMS variables will be replaced in random bits of PHP code, you need to use the actual date. Also, if '{start}' looks like '1346689843', you can't call strtotime on it. If you have PHP 5.3+, use the datetime library for stuff like this. Quote Link to comment https://forums.phpfreaks.com/topic/268971-taking-away-from-a-date/#findComment-1382098 Share on other sites More sharing options...
dreampho Posted October 1, 2012 Author Share Posted October 1, 2012 Okay, I have removed strotime and now $date is formatted correctly. But I dont understand how to remove the 14 days off this. Could you possibily give me an example? I have read in the php manual for datetime, but its throws an error when I try and use it. Again an example would be great. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/268971-taking-away-from-a-date/#findComment-1382107 Share on other sites More sharing options...
ManiacDan Posted October 1, 2012 Share Posted October 1, 2012 Stop using formatted dates, only format dates when you're ready to display them, it's not smart to continuously re-format and parse formatted dates throughout your code. php > $date = time(); php > $due_date = strtotime('-14 days', $date); php > echo date('c', $due_date); 2012-09-17T11:05:44-04:00 only use date() when you output. PHP has no way of telling if 20121001 is a date stamp or a formatted date-time string. Quote Link to comment https://forums.phpfreaks.com/topic/268971-taking-away-from-a-date/#findComment-1382118 Share on other sites More sharing options...
dreampho Posted October 1, 2012 Author Share Posted October 1, 2012 Thank you. I can see I was making it much more difficult than it needed to be. Quote Link to comment https://forums.phpfreaks.com/topic/268971-taking-away-from-a-date/#findComment-1382120 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.