date and time problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robin1
Forum Newbie
Posts: 20
Joined: Thu Aug 01, 2002 4:36 pm

date and time problem

Post by robin1 »

Hello everybody..
trying to add a number of days to the day1 variable to show the expired date but it's not working..
the example i followed:
http://www.phpbuilder.com/columns/akent ... hp3?page=5
but it's doesn't seem to work.. can someone look at this code and let me know what i'm doing wrong.

Code: Select all

<?
$timestamp = time();
echo "current time stamp -->: ".$timestamp;
echo "<p>";
$date_time_array = getdate($timestamp);
$day1 = $date_time_array["mday"];
$month1 = $date_time_array["month"];
$year1 = $date_time_array["year"];

echo "example 1 --> ".$month1." ".$day1." ".$year1."<p>"; //works
$date1 = mktime(0, 0, 0, $month1, $day1+15, $year1);

echo "<p>";
echo "exp date ". strftime("%A %d %b", $date1);
?>
Thanks
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try this - it just works with the timestamps and cuts out the intermediate getdate() stuff:

Code: Select all

<?php 

$timestamp = time(); 
echo '<p>current time stamp -->: '.$timestamp.'</p>'; 
echo '<p>current day -->: '.date('F d Y', $timestamp);

$expiry_ts = $timestamp + 15*24*60*60;
echo '<p>expiry timestamp: '.$expiry_ts.'</p>';
echo '<p>expiry date: '.date('F d Y', $expiry_ts);
?>
Mac
robin1
Forum Newbie
Posts: 20
Joined: Thu Aug 01, 2002 4:36 pm

Post by robin1 »

how would this work with mysql database.. i have the time() saved as int. in the db..
I tried copy/pasted the results of time() as what you see below but it displayed aug. 2, 2003 insted of 15 day exp. date
i tried $timestamp = "1058529275"; to test and it didn't work.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The output from the code should look something like this:

Code: Select all

current time stamp -->: 1057234186

current day -->: July 03 2003

expiry timestamp: 1058530186

expiry date: July 18 2003
The first timestamp is for todays date and the second for 15 days from today. You would get Aug 2 if you added 15 days to the second timestamp and '1058529275' is a version of the second timestamp.

Mac
Post Reply