Jump to content

Recommended Posts

Hey Everyone, Im new to this board and to PHP.

Im looking for help with a certain peice of of code that is crucial to the current site im working on.

 

I have created a customized news release where the customer can log in,enter a title and content, which then posts it in a stylized manner on the main website.

 

However he recently requested that there be a "Release on: [date]" and "Expire on: [date]"

 

Basically, he wants to be able to preload the database with news releases that are set to realease on a certain date, and then expire on another date. how would i go about doing this?

 

I would really appreciate some help on this  :)

 

Thanks Alot!

Link to comment
https://forums.phpfreaks.com/topic/50554-news-releasedate-help/
Share on other sites

Do you think you could write that in code so I can visually see what you mean?

 

You will want to use the DATE or DATETIME field in the MySQL database to store your date. Basically, this means that you need to format your date into a YYYY-MM-DD format for insert. Once you have the dates in the database, you can simply query for all records BETWEEN those two dates:

SELECT * FROM myTable WHERE CURDATE() BETWEEN startDate AND endDate;

 

Hope this helps point you in the right direction.

Link to comment
https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248441
Share on other sites

<?php
$releaseDate = strtotime("05/01/2007"); // should be retireved from teh database in some format
$expireDate = strtotime("05/08/2007"); // should be retireved from teh database in some format

// strtotime is used to convert the above to unix timestamps for comparison.

$today = time(); // unix timestamp of today

if ($releaseDate >= $today && $expireDate < $today) {
     print "The article should be printed here";
}else {
     print "The article has expired.";
}

// The above is the PHP version, the mysql version would be something like this.
// assuming the date is stored as an INT(11) in the database with 
// column names expiredate and releasedate that hold unix timestamps:
$getArticles = "SELECT * FROM table_articles WHERE releasedate >= " . $today . " AND expiredate < " . $today . " ORDER BY releasedate;";
$res = mysql_query($getArticles) or die(mysql_error());
while ($row = mysql_fetch_array($res)) {
  print $row['articlename']; // print article data here
}
?>

 

The mysql portion I am not sure if it allows the >=, but yea. That is "an example" of what the code would look like.

Link to comment
https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248454
Share on other sites

Do you think you could write that in code so I can visually see what you mean?

 

You will want to use the DATE or DATETIME field in the MySQL database to store your date. Basically, this means that you need to format your date into a YYYY-MM-DD format for insert. Once you have the dates in the database, you can simply query for all records BETWEEN those two dates:

SELECT * FROM myTable WHERE CURDATE() BETWEEN startDate AND endDate;

 

Hope this helps point you in the right direction.

 

I used this technique and it is working perfectly, expect for one thing... If I only enter a start date, and leave the expiry date blank ( so it will never expire) it will not display the nes because it defaults to 0000-00-00

 

how can i get around this?

Link to comment
https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248486
Share on other sites

I used this technique and it is working perfectly, expect for one thing... If I only enter a start date, and leave the expiry date blank ( so it will never expire) it will not display the nes because it defaults to 0000-00-00

 

how can i get around this?

 

I would use some sort of CASE statement in my query to test whether or not there is an endDate, and if not, just return tomorrow's date so it never expires:

SELECT *
FROM myTable
WHERE CURDATE() BETWEEN startDate AND (CASE
WHEN endDate = '000-00-00' THEN CURDATE + INTERVAL 1 DAY
ELSE endDate
END CASE)

Link to comment
https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248515
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.