Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

[web] php assign variable values into another variable

Started by ktiniatros 6 replies 750+ views
Original Post
ktiniatros
ktiniatros
Hey there, i have 3 intereg variables:$year,$month,$day. I want to form a new variable $date from the 3 above variables.I thought it would be something like $date="$year-$month-$day"; but it isnt.. Apparently i want to get a date variable,like YYYY-MM-DD. [Edited by - ktiniatros on December 21, 2005 7:26:37 PM]
Drew_Benton
Drew_Benton
Quote:
Original post by ktiniatros
Hey there,
i have 3 intereg variables:$year,$month,$day.
I want to form a new variable $date from the 3 above variables.I thought it would be something like $date="$year-$month-$day"; but it isnt..
Apparently i want to get a date variable,like YYYY-MM-DD.


Have you tried:
$date = $year. "-" . $month . "-" . $day; ?

And your method above should work, so make sure that the variables year, month and day are properly assigned. I jsut tried your code:
<?php $year = 1985;$month = 11; $day = 22; $date="$year-$month-$day";echo $date; ?>

On my web page, demo here and it works fine. So what's your exact code and your exact problem?
ktiniatros
ktiniatros
The problem is that i get a space after the year and the month so i cannot put it right into Mysql.My date becomes 2005 -12 -23.And then it becomes in mysql 0000-00-00.
That's because i take the 3 values from an xml i parse.
What can i do??
Drew_Benton
Drew_Benton
Quote:
Original post by ktiniatros
The problem is that i get a space after the year and the month so i cannot put it right into Mysql.My date becomes 2005 -12 -23.And then it becomes in mysql 0000-00-00.
That's because i take the 3 values from an xml i parse.
What can i do??


Well then it is working as it's supposed to, you just need to remove the spaces. What you can do is call the trim function on each variable to remove that extra whitespace.
$year = trim($year);$month = trim($month); $day = trim($day); 

You can read more info on the trim function here on PHP's web site.
konForce
konForce
$date = sprintf('%04d-%02d-%02d', $year, $month, $day);
ktiniatros
ktiniatros
Quote:
Original post by Drew_Benton
Quote:
Original post by ktiniatros
The problem is that i get a space after the year and the month so i cannot put it right into Mysql.My date becomes 2005 -12 -23.And then it becomes in mysql 0000-00-00.
That's because i take the 3 values from an xml i parse.
What can i do??


Well then it is working as it's supposed to, you just need to remove the spaces. What you can do is call the trim function on each variable to remove that extra whitespace.
$year = trim($year);$month = trim($month); $day = trim($day); 

You can read more info on the trim function here on PHP's web site.


thank u man.
i also works by casting the variables.e.x. $year = (int)$year; etc.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.