globex Posted January 2, 2008 Share Posted January 2, 2008 I have a variable with a date, for example: 2007-11-07 I want to just get the year, in this case: 2007. Is there a way to do this with some function? The only way I can figure it out is to to use substr to remove the last 6 digits. But then the variable is no longer a number, but a string and I can't perform operands on it. In which case is there a way to convert a string to an integer? Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/ Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 Did you look at the strtotime() function? Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428802 Share on other sites More sharing options...
phpQuestioner Posted January 2, 2008 Share Posted January 2, 2008 Did you look at the strtotime() function? left your link out there thrope. http://us2.php.net/manual/en/function.strtotime.php Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428804 Share on other sites More sharing options...
globex Posted January 2, 2008 Author Share Posted January 2, 2008 Err, not quite sure where to go from here. So I've converted it to a Unix timestamp... how do I get the year? Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428808 Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 <?php $v = '2007-11-07'; echo date("Y",strtotime($v)); ?> Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428810 Share on other sites More sharing options...
globex Posted January 3, 2008 Author Share Posted January 3, 2008 But this doesn't work: <?php $v = '2007-11-07'; $d = date("Y",strtotime($v)); echo $d - 1; ?> Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428819 Share on other sites More sharing options...
neilfurry Posted January 3, 2008 Share Posted January 3, 2008 Hi, i think you can make use of this format: <? $year = date('Y'); echo $year ; ?> for this will print the current year which is 2008. Hope this help. Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428820 Share on other sites More sharing options...
globex Posted January 3, 2008 Author Share Posted January 3, 2008 <?php $year = date('Y'); ?> Gives me the current year. I want it to give me the year from my string. Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428825 Share on other sites More sharing options...
neilfurry Posted January 3, 2008 Share Posted January 3, 2008 This work fine: <? $s ="2007-11-07"; $r =date('Y',strtotime($s)); print $r; ?> Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428827 Share on other sites More sharing options...
kenrbnsn Posted January 3, 2008 Share Posted January 3, 2008 <?php $v = '2007-11-07'; $d = date("Y",strtotime($v)); echo $d - 1;?> works fine for me. What are you attempting to do. If you want to get the previous year, you can do: <?php $v = '2007-11-07'; $d = date("Y",strtotime($v . '-1 year')); echo $d; ?> Ken Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428828 Share on other sites More sharing options...
neilfurry Posted January 3, 2008 Share Posted January 3, 2008 <? $s ="2007-11-07"; $r =date('Y',strtotime($s)); print $r; ?> Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428829 Share on other sites More sharing options...
globex Posted January 3, 2008 Author Share Posted January 3, 2008 Thanks Ken, it worked. I was just an idiot and running wrong functions afterwards. Link to comment https://forums.phpfreaks.com/topic/84209-solved-from-2007-11-07-to-just-2007/#findComment-428838 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.