Jump to content


Photo

php date help


  • Please log in to reply
3 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 05 July 2006 - 10:55 AM

Hi all

I have a website where users can sign up and they are given 30 days access to the site, I store the date the membership should expire in my sql database in the following format

2006-09-02

Currently I show in there user details the date their membership expires as

"Your membership expires on 2006-06-22"

A have a couple of questions. Firstly, how can I format the date to display it as

22/06/2006

I print it with the following code

<? print $membershiprow['myexpiredate']; ?>

And secondly, is it possible to count down the days until that date, so if the membership expired tomorrow then it would print the phase

"Your membership expires in 1 day"

Any help would be appricated as always

Thanks in advance

Dave

#2 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 05 July 2006 - 11:01 AM

http://www.php.net/date

Everything you need to know about date formating ;)

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 12:22 PM

To foramt the date and echo in how many days it is:
//$membershiprow['myexpiredate'] is something in the format of 2006-06-22
$old_format=$membershiprow['myexpiredate'];
$arr=explode("-",$old_format);
$new_format=$arr[2]."-".$arr[1]."-".$arr[0];
$time_expire=mktime(0,0,0,$arr[1],$arr[2],$arr[0]); //the timestamp of the date it expires
$sec_to_expire=$time_expire-time();
$days=floor($sec_to_expire/86400);
echo("Your membership expires on ".$new_format."<br>That's in ".$days." days!");
 

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 05 July 2006 - 01:49 PM

It's much easier to use the strtotime() function:
<?php
$days = floor((time() - strtotime($membershiprow['myexpiredate']))/86400);
echo 'Your membership expires on ' . date('d/m/Y',strtotime($membershiprow['myexpiredate'])) . "<br>That's in $days days!";
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users