Jump to content

[SOLVED] date and time help needed.


spires

Recommended Posts

Hi guys.

 

I have a date and time stored in my database: 24/06/2009 09:41:26

 

What I need to be able to do is see how much time has passed.

Example:

Todays date & time - Stored date & time = 01d 05h 58min has passed

 

i'm really stuck on this, does anyone of any date function that can do this?

 

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/163470-solved-date-and-time-help-needed/
Share on other sites

Hi,

 

Your code works perfect.

However, at the moment it counts down, how do I get it to count up?

 

I want to put in a start date, If I do that in your code, I get -3 days, rather than 3 days.

Please see: http://www.businessmobiles.com/comcalc/test4.php

 

Your code:

$subEnd = date("6/22/2009 11:08:30");

$timeLeft = (strtotime($subEnd) - time());
$mins = floor($timeLeft / 60);
$secs = $timeLeft - ($mins * 60);

$hours = floor($mins / 60);
$mins = $mins - ($hours * 60);

$days = floor($hours / 24);
$hours = $hours - ($days * 24);

echo "You have {$days} days, {$hours} hours, {$mins} minutes and {$secs} seconds remaining of your subscription";

 

 

Thanks

Firstly, you should store your date-time values in a DATETIME type field (YYYY-MM-DD HH:MM:SS). As far as I know the MySQL functions doesn't recognize your current format, and I know that strtotime() doesn't. Alternatively you'd have to rearrange your format into the above format:

 

$old_format = '24/06/2009 09:41:26';
$parts = explode(' ', $old_format);
$parts[0] = implode('-', array_reverse(explode('/', $parts[0])));
$new_format = implode(' ', $parts);

 

Then you could use Russell's code, but rearrange strtotime($subEnd) - time() into time() - strtotime($subEnd), where $subEnd is the date-time value.

thebadbad beat me to the punch, I was gonna say to switch those values, however, I was bickering with newegg, do you believe they don't let you use alternative mailing addresses with paypal.. so now I need to wait 5 freaking days for a refund!!!!! argh!! so aggravating!! but, what can I do, they gave me a $5 discount on my next purchase.. I'll use the money I save @ the pump.. and get 2 gallons of gas ahahaha..

Solved. Thanks guys :)

 

$subEnd = date("m/d/Y H:i:s");

$timeLeft = (strtotime($subEnd) - strtotime($notes_date));

$mins = floor($timeLeft / 60);
$secs = $timeLeft - ($mins * 60);

$hours = floor($mins / 60);
$mins = $mins - ($hours * 60);

$days = floor($hours / 24);
$hours = $hours - ($days * 24);

$diffDate = "{$days}d, {$hours}h, {$mins}min";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.