Jump to content

Show message every 24 hours


JustMike

Recommended Posts

Hi, I'm just started to learn PHP and now I encountered a problem.

 

I'm trying to show a message every 24h but I can't figure it out.

Here is my code:

$link = mysql_connect('localhost', 'root', '');if (!$link) {    die('Not connected : ' . mysql_error());}$db_selected = mysql_select_db('gifts', $link);if (!$db_selected) {    die ('Can't use foo : ' . mysql_error());}$sel = mysql_query("SELECT * FROM xmas") or die(mysql_error());	$row = mysql_fetch_assoc($sel);	$time_now = date('y-m-d h:i:s', time());//var_dump($time_now);		if($time_now>$row['date']){			echo "here's your gift ";		}else{			echo "next gift tomorrow!";		}

I know I should use mysqli or PDO because the mysql will be deprecated, but I started to learn this way and after I get the basics I will change to one of those.

 

How can I solve this? What I'm missing?

Thanks in advance for your help!

Link to comment
Share on other sites

I don't have acces to cron jobs on the server where the website is hosted. I was thinking that I can check if the $row['date']+86400<time() or somthing similar will work but it seems that I can't figure it out.I forgot to mention, the xmas table looks like this:

  `id` int(11) NOT NULL AUTO_INCREMENT,  `date` datetime NOT NULL,  `gift` varchar(255) NOT NULL,

I think I found a solution, I don't know if this is the best option but here is my new code:

I changed the type of date in the database to DATE and here is my new code:

$day = date('Y-m-d');$lastGift = $row['date'];$disabled = 0;	if ($lastGift <= $day){  $disabled = 1; }if($disabled == 0){	echo "here's your gift ";}else{	echo "next gift tomorrow!";}
Edited by JustMike
Link to comment
Share on other sites

I know, it's a weird thing,but the hosting is free (from a friend with a server home) and I don't use this thing just a few days a year....anyway, I,ll put my full code when I get home...maybe other users needs this thing too.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...