How do I convert 1800 into a TIME, and then deduct 20 mins?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I convert 1800 into a TIME, and then deduct 20 mins?

Post by simonmlewis »

We are booking in times, and need to instruct people to arrive 20 minutes before the specified time.

While I can just say that, it would look better to say "please arrive by 1740"... but how do I convert 1800 into a time, and then deduct the 20 minutes?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by Celauran »

DateTime::modify

Code: Select all

$date = new DateTime('2012-05-08 18:00');
$date->modify('-20 minutes');
echo $date->format('F j, Y H:i');
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by simonmlewis »

$date = new DateTime('$row1->entrydate $row1->entrytime');
$date->modify('-20 minutes');
echo $date->format('F j, Y H:i');

This is throwing up a load of errors.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by Celauran »

The code is fine. What errors are you getting? What version of PHP?

EDIT: Also, remove the single quotes around $row->whatever
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by simonmlewis »

[text]Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: Failed to parse time string ($row1->entrydate $row1->entrytime) at position 0 ($): Unexpected character' in C:\xampp\phpMyAdmin\site\includes\search.inc:43 Stack trace: #0 C:\xampp\phpMyAdmin\site\includes\search.inc(43): DateTime->__construct('$row1->entrydat...') #1 C:\xampp\phpMyAdmin\site\index.php(89): include('C:\xampp\phpMyA...') #2 C:\xampp\phpMyAdmin\site\index.php(237): getPage() #3 {main} thrown in C:\xampp\phpMyAdmin\site\includes\search.inc on line 43[/text]

Seems it doesn't like me putting "$row1->entrytime" etc into the brackets.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by Celauran »

Code: Select all

$date = new DateTime($row1->entrydate .' '. $row1->entrytime);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by simonmlewis »

Bingo - brilliant. Tho I don't quite understand thsoe differences - tho I do see them.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by Celauran »

Wrapped in single quotes, the variables weren't parsed. You were passing the literal string '$row->entrydate $row->entrytime', which isn't a valid date, so DateTime::__construct threw an exception. You need to remove the single quotes so the variables are parsed, but also need to concatenate a space between them so they're read as a single argument.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I convert 1800 into a TIME, and then deduct 20 mi

Post by simonmlewis »

Ok, thank you. I also learned here how to remove the date part of what's echoed, so it's only 1740.

Ta.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply