TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-13-2007, 07:07 PM   #1 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default Most efficient way of playing with dates?

So I've got this unix timestamp from the past which I needed to convert into a date:
PHP Code:
$firstDate date('Y-m-d H:i'1195958278); 
Next I added 90 days:
PHP Code:
$expiry date('Y-m-d H:i'strtotime('+90 day'$firstDate)); 
This is because each pass is valid for 90 days after the first date. I then proceed by figuring out how many days are left for the pass to expire:
PHP Code:
$daysLeft 365 - (date('z'strtotime(date('Y-m-d H:i'))) - date('z'strtotime($expiryDate))); 
I've tested it with a bit of data and it seems to work.

Does anyone see something totally wrong? Or maybe an easier alternative?

Thank you.
bdm is offline  
Reply With Quote
Old 12-13-2007, 07:38 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I dont use strtotime, unix timestamps are seconds since the unix epoch (1/1/1970), so 90 days from now would be $time + (60*60*24*90), the remainder of days would be time - $original_time / (60/60/24)

Just my preference.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-13-2007, 07:57 PM   #3 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 7
Thanks: 2
kevthedude is on a distinguished road
Default

This is how I modified your code to work:
PHP Code:
$first 1195958278;
$last mktime(000date("m"$first)  , date("d"$first)+90date("Y"$first));
$today mktime(000date("m")  , date("d"), date("Y"));
$remaining floor(($last $today)/86400);
echo 
remaining
$remaining will be your days left. You may have to switch the floor() function to ceil()... I didn't figure out the real difference in the dates but this script shows 71 days remaining. Tell me if this is wrong and I'll fix it up for you.
kevthedude is offline  
Reply With Quote
Old 12-13-2007, 08:05 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

As an aside, you don't need to do date('z', strtotime(date('Y-m-d H:i'))) to get today's day of the year. Simply skip the second argument to use the current time stamp: date('z').

Personally, I'd prefer working with the timestamps themselves and only use the date() function for outputting friendly strings. Something like the the following:
php Code:
<?php

$iDateStart   = 1195958278;

// expiry = starting timestamp + 90 days (in seconds)
$iDateExpire  = $iDateStart + (90 * 24 * 60 * 60);

// Days left = difference between timestamps in seconds (expiry - now)
//             divided by number of seconds in a day (86,400)
// Use floor() to round down to the day.
$iDaysLeft    = floor(($iDateExpire - time()) / (24 * 60 * 60));

// Output:
// There are 71 days left until 2008-02-23 02:37.
printf('There are %d days left until %s.',
       $iDaysLeft,
       date('Y-m-d H:i', $iDateExpire));
Salathe is offline  
Reply With Quote
Old 12-13-2007, 11:40 PM   #5 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Personally, I'd prefer working with the timestamps themselves and only use the date() function for outputting friendly strings.
Quoted in agreement.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 12-14-2007, 07:46 AM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

The most efficient way of working with dates in PHP it's by operating on the date timestamp and just output the formatted one (yes I am agreeing with Salathe) :D
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 12-14-2007, 01:36 PM   #7 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Thanks for everyones input.

Now that I think about it, playing with the timestamp itself seems better.

bdm is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:39 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design