943,169 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 32
  • PHP RSS
Aug 31st, 2010
0

php dates

Expand Post »
is it possible to use a vaiable within the date() insted of using y-m-d. i want to use $day-$month-$year
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gchurch is offline Offline
52 posts
since Aug 2010
Aug 31st, 2010
0
Re: php dates
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:

PHP Syntax (Toggle Plain Text)
  1. echo ''.$month.'/'.$date.'/'.$year.'';
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jkaye is offline Offline
11 posts
since Jul 2010
Aug 31st, 2010
0
Re: php dates
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 Syntax (Toggle Plain Text)
  1. <?php
  2. $day = 'd';
  3. $month = 'm';
  4. $year = 'Y';
  5.  
  6. $format = "$day-$month-$year"; // d-m-Y
  7. 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 Syntax (Toggle Plain Text)
  1. <?php
  2. $day = '31';
  3. $month = '8';
  4. $year = '2010';
  5.  
  6. $format = "$day-$month-$year"; // 31-8-2010
  7. echo date('d-m-Y', strtotime($format)); // 31-08-2010
  8. echo strtotime($format); // 1283308069 or something

php Syntax (Toggle Plain Text)
  1. <?php
  2. $day = '31';
  3. $month = '8';
  4. $year = '2010';
  5.  
  6. $format = "$day-$month-$year"; // 31-8-2010
  7. $date = new DateTime($format);
  8. echo $date->format('d-m-Y'); // 31-8-2010
  9. echo $date->getTimestamp(); //1283308069 or something

Hopefully this helps.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
623 posts
since Jul 2008
Sep 1st, 2010
0
Re: php dates
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gchurch is offline Offline
52 posts
since Aug 2010
Sep 1st, 2010
0
Re: php dates
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gchurch is offline Offline
52 posts
since Aug 2010
Sep 1st, 2010
0
Re: php dates
got it!!!!!!!

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

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

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How To Add a Count Value to a Foreach Statement?
Next Thread in PHP Forum Timeline: Making of Hindi WebSite





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC