Solved

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

Posted on 2016-09-25
8
10 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 500 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
 

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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

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

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
 

Author Closing Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now