Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can't add hours to date and time stored in variable

Posted on 2016-09-25
8
Medium Priority
?
20 Views
Last Modified: 2016-09-30
if I do this:

echo date("y-m-d h:i:sa", strtotime('+24 hour'));

Open in new window


it works as expected. But I am trying to add 24 hours to the date and time stored in the database which isn't working.

I have tried something like:

$register_date = $row['register_date'];
echo $register_date, strtotime('+24 hour');

Open in new window


but that just shows me the registered date from the database and a unix timestamp.

I am pretty stumped on this one.


-OR-

Should I just use an integer instead of date/time? The user has 24 hours to activate their account otherwise they will need to generate a new code.
0
Comment
Question by:Black Sulfur
  • 4
  • 4
8 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41814593
You could probably do it in the database... are you using MySQL?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 2000 total points
ID: 41814594
If so, this might work:

SELECT DATE_ADD(register_date, INTERVAL 24 HOUR) 
FROM my_table;

Open in new window

0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41814597
Hi Terry,

I am using mysql and PHP. My select statement currently looks like this (prepared)

$stmt = $link->prepare("SELECT `register_date` FROM `db_users` WHERE `user_email` = ?");

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Black Sulfur
ID: 41814608
So, I changed it to:

$stmt = $link->prepare("SELECT DATE_FORMAT(register_date, '%e-%m-%y %r') AS test FROM `db_users` WHERE `user_email` = ?");

Open in new window


which looks exactly the same as
$date_now = date("d-m-y h:i:sa");

Open in new window


The only difference is that one has pm in lower case and the other in uppercase PM, for some reason.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41814944
Did you try DATE_ADD as I suggested above?
0
 
LVL 1

Accepted Solution

by:
Black Sulfur earned 0 total points
ID: 41814949
It was getting a bit confusing with the prepared statements. I found a solution though which makes working with times and dates much easier and I think I got it now. I used Carbon which is awesome!

             
  $db_date = $row['register_date'];
		$carbon = new Carbon($db_date);
		$expiry_date = $carbon->copy()->addDays(1);
		$now = Carbon::now();

                         if($now > $expiry_date) {
			
			     header("location:expired.php");
		                  }

Open in new window


http://carbon.nesbot.com/docs/
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41814976
Nice work! Perhaps you can accept that as the solution, with mine as Assisted.
0
 
LVL 1

Author Closing Comment

by:Black Sulfur
ID: 41823172
Found a solution using Carbon through my own research
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question