Page 1 of 1

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

Posted: Tue May 08, 2012 9:36 am
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?

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

Posted: Tue May 08, 2012 9:46 am
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');

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

Posted: Tue May 08, 2012 9:54 am
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.

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

Posted: Tue May 08, 2012 9:56 am
by Celauran
The code is fine. What errors are you getting? What version of PHP?

EDIT: Also, remove the single quotes around $row->whatever

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

Posted: Tue May 08, 2012 9:57 am
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.

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

Posted: Tue May 08, 2012 9:58 am
by Celauran

Code: Select all

$date = new DateTime($row1->entrydate .' '. $row1->entrytime);

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

Posted: Tue May 08, 2012 9:59 am
by simonmlewis
Bingo - brilliant. Tho I don't quite understand thsoe differences - tho I do see them.

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

Posted: Tue May 08, 2012 10:02 am
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.

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

Posted: Tue May 08, 2012 10:05 am
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.