swatisonee Posted November 21, 2006 Share Posted November 21, 2006 I have been using [code]$Month = substr($visit_date, 6, 2);[/code]for all my forms to extract 09 from say 2006-09-08 . However, i have just realised that this month ie November , date 2006-11-20 is returning -1 and not 11 .Why could this be and what should i change it to ?Thanks !Swati Link to comment https://forums.phpfreaks.com/topic/27961-sudden-substr-issue/ Share on other sites More sharing options...
printf Posted November 21, 2006 Share Posted November 21, 2006 all strings start at offset 0, so it would [b]5[/b] instead of [b]6[/b]printf Link to comment https://forums.phpfreaks.com/topic/27961-sudden-substr-issue/#findComment-127900 Share on other sites More sharing options...
kenrbnsn Posted November 21, 2006 Share Posted November 21, 2006 If you have the date in that format, why use substr()? You can used a combination of the [url=http://www.php.net/date]date()[/url] and [url=http://www.php.net/strtotime]strtotime()[/url] functions:[code]<?php$visit = '2006-11-20';$Month = date('m',strtotime($visit));?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/27961-sudden-substr-issue/#findComment-127905 Share on other sites More sharing options...
swatisonee Posted November 21, 2006 Author Share Posted November 21, 2006 Ken, PrintfThanks. I find it odd that this has cropped up only in November . Could it be that the data looks at -1 and sees January ?[code]$visit_date= $_POST["visit_date"];$Month = substr($visit_date, 6, 2);[/code] Link to comment https://forums.phpfreaks.com/topic/27961-sudden-substr-issue/#findComment-127909 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.