Jump to content

Recommended Posts

//$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

Link to comment
https://forums.phpfreaks.com/topic/290469-php-date-conversion-problem/
Share on other sites

 

 

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.

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.