tellyphp Posted August 15, 2014 Share Posted August 15, 2014 //$start_point = '2014-07-22'; $lastday = date('t',strtotime($start_point)); $month_val = date('n',strtotime($start_point)); echo "Last day: ". $lastday . "<br/>"; echo "Month: ". $month_val . "<br/>"; I have a problem with a date value read from a MySQL database table into a PHP variable but when manipulating the vale to determine the month of the date, unexpected results appear. I am not sure what causes this but I suspect PHP is not properly handling date to string conversion. The output from the code shows 1 as the month when it should be 7. Hower the line of code with the comment "//$start_point = '2014-07-22' if added in give the correct result. When the value of the variable "start_point" is printed, it gives the value "2014-07-22". Please see the code Telly Quote Link to comment https://forums.phpfreaks.com/topic/290469-php-date-conversion-problem/ Share on other sites More sharing options...
boompa Posted August 15, 2014 Share Posted August 15, 2014 Did you know there are MySQL functions for both of those? You may just be able to use those. https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html Quote Link to comment https://forums.phpfreaks.com/topic/290469-php-date-conversion-problem/#findComment-1487878 Share on other sites More sharing options...
Ch0cu3r Posted August 15, 2014 Share Posted August 15, 2014 The output from the code shows 1 as the month when it should be 7. Hower the line of code with the comment "//$start_point = '2014-07-22' if added in give the correct result. When the value of the variable "start_point" is printed, it gives the value "2014-07-22". What? You are expecting that code to return 7 for the month even with the first line is comment out? You do realise lines that are commented out are ignored. Uncomment the first line returns the correct result. Quote Link to comment https://forums.phpfreaks.com/topic/290469-php-date-conversion-problem/#findComment-1487879 Share on other sites More sharing options...
ginerjm Posted August 15, 2014 Share Posted August 15, 2014 How about the query statement that retrieved the date? Echo that out first to be sure you are getting what you expect. Of course, as pointed out, using MySQL's DATE_FORMAT function would do all this work for you. Quote Link to comment https://forums.phpfreaks.com/topic/290469-php-date-conversion-problem/#findComment-1487881 Share on other sites More sharing options...
tellyphp Posted August 17, 2014 Author Share Posted August 17, 2014 Ch0cu3r mentioned uncommenting the line in the code that assigns the date, but that I only included for testing purposes. The start_point variable eqarlier in the code already has a value (which is the same date in the uncommented code). It is juts that it works as a string, when the code is uncommented, but when ready as a date value from MySQL it does not. Thanks for the information on the MYSQL functions, but I really want to get this done from within PHP code. Telly Quote Link to comment https://forums.phpfreaks.com/topic/290469-php-date-conversion-problem/#findComment-1487982 Share on other sites More sharing options...
mac_gyver Posted August 17, 2014 Share Posted August 17, 2014 it's likely that the actual data you are getting from your database query isn't what it looks like or you have a variable scope problem. when you printed the $start_point value, where exactly in your code did you do that and what does using var_dump($start_point);, right before the code you have shown us, give for the value? Quote Link to comment https://forums.phpfreaks.com/topic/290469-php-date-conversion-problem/#findComment-1487983 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.