Jump to content

php date help


CanMan2004

Recommended Posts

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

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

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
Link to comment
Share on other sites

To foramt the date and echo in how many days it is:
[code=php:0]//$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!");[/code]
[hr]

Orio.
Link to comment
Share on other sites

It's much easier to use the strtotime() function:
[code]<?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!";
?>[/code]

Ken
Link to comment
Share on other sites

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.