Jump to content

[SOLVED] I need the idea of calculating time !


samoi

Recommended Posts

hello guys

 

the topic may look strange !

 

here is what I mean in a simple example!

 

if I wake up at 7:55 AM,

and I decide to sleep at 12:34 AM.

 

I want to make a script that help me calculate the exact time that I will have for sleeping !

 

I gave it a try since I want to learn, but I'm still a beginner ! So, I made this

 

<?php

$sleep = date("12:34");
$wakeup = date("07:55")

$time_remaining = $sleep - $wakeup ;

echo $time_remaining;

?>

 

I know this is completely wrong in format and logically.

 

but I want to make the idea clear

 

Thank you a lot

Link to comment
Share on other sites

The first problem with the above is that 12:34 AM would be represented as 00:34. 12:34 would be 12:34 PM.

 

So, assuming you are using 24 hour time, this would work (although I'm sure there is something more efficient):

 

function timeDiff($startTime, $endTime)
{
    $startTimes = explode(':', $startTime);
    $endTimes   = explode(':', $endTime);
    $startMinutes = ($startTimes[0] * 60) + $startTimes[1];
    $endMinutes   = ($endTimes[0]   * 60) + $endTimes[1];

    if ($endMinutes > $startMinutes)
    {
        $diffMinutes = ($endMinutes - $startMinutes);
    }
    else
    {
        $diffMinutes = ((24*60) - $startMinutes + $endMinutes);
    }

    $hours   = str_pad(floor($diffMinutes/60), 2, 0, STR_PAD_LEFT);
    $minutes = str_pad($diffMinutes-($hours * 60), 2, 0, STR_PAD_LEFT);

    return "{$hours}:{$minutes}";
}

$wake = '00:34';
$sleep   = '07:55';

echo timeDiff($sleep, $wake); // 16:39

Link to comment
Share on other sites

The first problem with the above is that 12:34 AM would be represented as 00:34. 12:34 would be 12:34 PM.

 

So, assuming you are using 24 hour time, this would work (although I'm sure there is something more efficient):

 

function timeDiff($startTime, $endTime)
{
    $startTimes = explode(':', $startTime);
    $endTimes   = explode(':', $endTime);
    $startMinutes = ($startTimes[0] * 60) + $startTimes[1];
    $endMinutes   = ($endTimes[0]   * 60) + $endTimes[1];

    if ($endMinutes > $startMinutes)
    {
        $diffMinutes = ($endMinutes - $startMinutes);
    }
    else
    {
        $diffMinutes = ((24*60) - $startMinutes + $endMinutes);
    }

    $hours   = str_pad(floor($diffMinutes/60), 2, 0, STR_PAD_LEFT);
    $minutes = str_pad($diffMinutes-($hours * 60), 2, 0, STR_PAD_LEFT);

    return "{$hours}:{$minutes}";
}

$wake = '00:34';
$sleep   = '07:55';

echo timeDiff($sleep, $wake); // 16:39

 

Thank you very much, I really appreciate it !

 

and please if you can help me with a tutorial of such thing to help me understanding how to do such problems like this.

 

You are very helpful, thank you a lot!

Link to comment
Share on other sites

The best method I can think of is to use a 24 hour time format and then just subtract. For example:

 

<?php
// Converts a 12 hour string like "6:34 PM" to a 24 hour string like "18:34"
function toAstronomicalTime($time) {
// Break apart the string
preg_match('#^([0-9]{1,2})[0-9]{1,2})\s*(P|A)M$#', $time, $matches);

// Error if it's a bad string
if(empty($matches)) {
	throw new Exception('Invalid Time');
}

// See if it's PM, if so add 12 to the hours
if($matches[3] == 'P') {
	$matches[1] += 12;
}
// If it's 12 AM make hours 0
elseif($matches[1] == '12') {
	$matches[1] = 0;
}

return $matches[1] . ':' . $matches[2];
}

// Make our test times
$sleep = '11:55 PM';
$wake = '7:55 AM';

// Convert the times to 24 hour and split them up into their component hours and minutes
list($sleepHr, $sleepMin) = explode(':', toAstronomicalTime($sleep));
list($wakeHr, $wakeMin) = explode(':', toAstronomicalTime($wake));

// Find the differences
$hrDiff = $wakeHr - $sleepHr;
$minDiff = $wakeMin - $sleepMin;

// If hour went negative (such as 11PM - 8PM), adjust by adding 24 hours
if($hrDiff < 0) {
$hrDiff += 24;
}

// If min went negative (such as 12:32 - 12:34), adjust by adding 60 mins
// and subtract one from the hours
if($minDiff < 0) {
$minDiff += 60;
$hrDiff -= 1;
}

// Print it out
echo sprintf('%02s:%02s', $hrDiff, $minDiff);

 

edit: Someone already posted, but oh well I'll post mine anyway. Mine calculates the time that the person was asleep, which is what I thought the OP wanted :) oh well again!

Link to comment
Share on other sites

The best method I can think of is to use a 24 hour time format and then just subtract. For example:

 

<?php
// Converts a 12 hour string like "6:34 PM" to a 24 hour string like "18:34"
function toAstronomicalTime($time) {
// Break apart the string
preg_match('#^([0-9]{1,2})[0-9]{1,2})\s*(P|A)M$#', $time, $matches);

// Error if it's a bad string
if(empty($matches)) {
	throw new Exception('Invalid Time');
}

// See if it's PM, if so add 12 to the hours
if($matches[3] == 'P') {
	$matches[1] += 12;
}
// If it's 12 AM make hours 0
elseif($matches[1] == '12') {
	$matches[1] = 0;
}

return $matches[1] . ':' . $matches[2];
}

// Make our test times
$sleep = '11:55 PM';
$wake = '7:55 AM';

// Convert the times to 24 hour and split them up into their component hours and minutes
list($sleepHr, $sleepMin) = explode(':', toAstronomicalTime($sleep));
list($wakeHr, $wakeMin) = explode(':', toAstronomicalTime($wake));

// Find the differences
$hrDiff = $wakeHr - $sleepHr;
$minDiff = $wakeMin - $sleepMin;

// If hour went negative (such as 11PM - 8PM), adjust by adding 24 hours
if($hrDiff < 0) {
$hrDiff += 24;
}

// If min went negative (such as 12:32 - 12:34), adjust by adding 60 mins
// and subtract one from the hours
if($minDiff < 0) {
$minDiff += 60;
$hrDiff -= 1;
}

// Print it out
echo sprintf('%02s:%02s', $hrDiff, $minDiff);

 

edit: Someone already posted, but oh well I'll post mine anyway. Mine calculates the time that the person was asleep, which is what I thought the OP wanted :) oh well again!

 

Oh guys I can't think of this whole way !

 

Thank you very much, and you're all good you both helped me alot and you gave me the chance to learn more :)

 

yours is more clear and has alot of descriptions !thank you

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.