Solved

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

Posted on 2016-09-25
8
17 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

623 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