954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php dates

is it possible to use a vaiable within the date() insted of using y-m-d. i want to use $day-$month-$year

gchurch
Junior Poster in Training
52 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

I don't think so. Just from using it enough, I believe it takes the format from the date(), so a variable would do nothing. If you want to print a date with your variables, I would suggest doing something such as this:

echo ''.$month.'/'.$date.'/'.$year.'';
jkaye
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 2
 

If the value of $day-$month-$year evaluates to a string like 'd-m-Y' or any other combination of php date formats then yes you could.

<?php
$day = 'd';
$month = 'm';
$year = 'Y';

$format = "$day-$month-$year"; // d-m-Y
echo date($format); // 31-08-2010


If the value of $day-$month-$year evaluates to an actual date string like '31-8-2010' then no you can not feed that to the date function.

You would need to use strtotime or the DateTime object. I would suggest the later as it is compatible with dates after 2038, among countless other benefits.

<?php
$day = '31';
$month = '8';
$year = '2010';

$format = "$day-$month-$year"; // 31-8-2010
echo date('d-m-Y', strtotime($format)); // 31-08-2010
echo strtotime($format); // 1283308069 or something
<?php
$day = '31';
$month = '8';
$year = '2010';

$format = "$day-$month-$year"; // 31-8-2010
$date = new DateTime($format);
echo $date->format('d-m-Y'); // 31-8-2010
echo $date->getTimestamp(); //1283308069 or something


Hopefully this helps.

mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 

thanks hey

$format = "$day-$month-$year"; // 31-8-2010
echo date('d-m-Y', strtotime($format)); // 31-08-2010
echo strtotime($format); // 1283308069 or something

------> 04-09-20101283558400

$format = "$day-$month-$year"; // 31-8-2010
$date = new DateTime($format);
echo $date->format('d-m-Y'); // 31-8-2010
echo $date->getTimestamp(); //1283308069 or something

------> 04-09-20101283558400

thanks for the two above they both return the same date value's for the data that was entered. however is it possible to have all the numbers after 2010 to disapear?
and maybe have it in 04 Sept 2010 format???

been tryin but getting nowhere. if you can help i would greatly appriciate it.

gchurch
Junior Poster in Training
52 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

well got the date to look like 04 Sept 2010 however still have the time stamp after.

is there anyway of me getting rid of it???

gary

gchurch
Junior Poster in Training
52 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

got it!!!!!!!

just needed to take out the one 'echo'. hahaha

thanks

gchurch
Junior Poster in Training
52 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You