This is for my OJT project. I need to search birthday from MySQL database in DATE type. I already got the query from January to December. The December to January is the problem.
This is what i've done code so far:
<?php
require 'connect.php';
?>
<?php
if(isset($_POST['birthday_from']) && isset($_POST['birthday_to'])){
$start_date=$_POST['birthday_from'];
$end_date=$_POST['birthday_to'];
//$s = strtolower($end_date);
$start_year = date('m-d', strtotime($start_date));
$end_year = date('m-d', strtotime($end_date));
$search_year = date('Y', strtotime($start_date));
$search_year2 = date('Y', strtotime($end_date));
$sql=" select * from members where (DATE_FORMAT(`birthday`, '%m-%d') BETWEEN '{$start_year}' AND '{$end_year}') order by DATE_FORMAT(`birthday`, '%m-%d') asc ";
$result = mysql_query($sql);
$num_row = mysql_num_rows($result);
if($num_row >= 1){
echo '<div class="alert alert-info" style="margin:auto;width:900px;"><b><center>BIRTHDAY RESULT</center></b></div>';
while($rows = mysql_fetch_array($result)){
$date = date('Y-m-d')-$rows['birthday'];
echo '<b><span style="margin-left:20px;">Name:</span></b> '.strtoupper($rows['firstname'].' '.$rows['middlename'].' '.$rows['lastname'].' '.$rows['suffix']).'<br/>';
echo '<b><span>Birthday:</span></b> '.$rows['birthday'].'<br/>';
echo '<b><span style="margin-left:30px;">Age: </span></b>'.$date.'<br/><br />';
}
}
}
?>
You do not check the case when start date > end date. It is happens.
You named var $start_year = date('m-d', strtotime($start_date)), but there is no any Year in value. It is bad practice.
You do not use vars search_year, search_year2 after defining and assigning.
I do not understand what You want, can You give the example with several rows in table and result what You want to get but You cannot?
Looks like you're using the wrong variables for your BETWEEN statement.
BETWEEN '{$start_year}' AND '{$end_year}'
should be:
BETWEEN '{$seach_year}' AND '{$search_year2}'
Related
I'm trying to use one's date of birth to calculate when he'll be 50, if he's not 50 already.
If person is not 50, add a year to his age then check if it'll be 50. If not, iterate until it's true. Then get the date he turned 50. in PHP
Here's the code, not complete.
$rAge = 50;
$retir = date('j F Y ', strtotime("+30 days"));
$oneMonthAdded = strtotime(date("d-m-Y", strtotime($DOB)). "+1 year");
$re = date("d-m-Y", $oneMonthAdded);
$futDate = date("d-m-Y", strtotime(date("d-m-Y", strtotime($re))));
$date_diff = strtotime($futDate)-strtotime($DOB);
$future_age = floor(($date_diff)/(60*60*24*365));
Help please.
try this code bro!
// your date of birth
$dateOfBirth = '1950-11-26';
// date when he'll turn 50
$dateToFifty = date('Y-m-d', strtotime($dateOfBirth . '+50 Years'));
// current date
$currentDate = date('Y-m-d');
$result = 'retired';
// checks if already fifty
if($currentDate <= $dateToFifty) {
$result = $dateToFifty;
}
echo $result;
I use simplest php code to find out retire date. y
<?php
$dob = '1970-02-01';
$dob_ex = explode("-",$dob);
$age_diff = date_diff(date_create($dob), date_create('today'))->y;
$year_of_retire = 50 - $age_diff;
$end = date('Y', strtotime('+'.$year_of_retire.'years'));
$date_of_retire = $end."-".$dob_ex[1]."-".$dob_ex[2];
echo $date_of_retire;
?>
you can use if...else condition to echo values according to you.
like
if($year_of_retire > 0){
echo $date_of_retire;
} else if($year_of_retire < 0){
echo "retired";
}
I can't figure it out! I've created an array of months in "Slovenian language" and now, I would want to display my month's name instead of the number. Instead of working, it writes out - 32014vEurope/Berlin11bEurope/BerlinWed and some more weird stuff, it should obviously print out November in my case. I would like to solve this problem with arrays, but It just wouldn0t convert the number of 'n' to the requested month.
function kliknjena($link, $mojster)
{
$meseci[1] = "Januar";
$meseci[2] = "Februar";
$meseci[3] = "Marec";
$meseci[4] = "April";
$meseci[5] = "Maj";
$meseci[6] = "Junij";
$meseci[7] = "Julij";
$meseci[8] = "Avgust";
$meseci[9] = "September";
$meseci[10] = "Oktober";
$meseci[11] = "November";
$meseci[12] = "December";
$sql = "SELECT naslov, podnaslov, vsebina, ustvarjeno, slug FROM novica
where slug = '$mojster'
limit 1";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<h1>".$row["naslov"]."</h1>";
$timestamp = strtotime($row["ustvarjeno"]);
$m = date("n", $timestamp);
echo date("d. $meseci[$m]; Y, H:i:s", $timestamp);
echo "<p>".$row["podnaslov"]."</p>"."<br>";
echo "<p>".$row["vsebina"]."</p>"."<br>";
}
}
else
{
echo "0 results";
}
}
Use:
echo date('d. ', $timestamp) . $meseci[$m] . date('; Y, H:i:s', $timestamp);
What's happening is that the month name is being substituted into the date() argument, and then all the letters in the month are being treated as formatting characters, so they get replaced with the corresponding fields from the date and time.
Hi firstly heres my code.
<?php
function getDatesBetween2Dates($startTime, $endTime) {
$day = 86400;
$format = 'd-m-Y';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$numDays = round(($endTime - $startTime) / $day) + 1;
$days = array();
for ($i = 0; $i < $numDays; $i++) {
$days[] = date($format, ($startTime + ($i * $day)));
}
return $days;
}
///
$days = getDatesBetween2Dates(date('d-m-Y', strtotime('-3 weeks Monday')),date('d-m-Y', strtotime('+2 weeks Sunday')));
foreach($days as $key => $value){
$dayNumber = date('d', strtotime($value));
//echo $value;
echo "<div id=\"day\">
<div id=\"number\">$dayNumber</div>";
////////////sql seearch//\\\/////////
//Connect to db
include("../djwbt.php");
$sql = "SELECT * FROM daysummary WHERE date='$value'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$place = $row['place'];
$invoicedate = $row['date'];
}
/////////////end sql search//////////
echo "<div id=\"event\">$place</div>
</div><!-- end day -->";
}
?>
What i am trying to do is show all dates between two points and for each of the dates search my db using the date as a where clause. i have tried putting the search in a few places but im not getting the right results.
this gives me the same result in each date.
e.g. 17th = (empty) as in my db, 18TH = HOME (as in my db), 19th = HOME (not as in my db), 20th = HOME (this continues all the way through fore each)
the link in each fore each works perfectly?
Any help would be amazing.
I would make one statement that gets all the needed data from your database:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
then use the foreach loop for the results
Note that mysql_ functions are deprecated, Try switching to mysqli_ or PDO
I have a series of weekly events in a database along with the day they happen on (in full form, so: 'Monday', 'Tuesday' etc). I've successfully printed the events in a while loop ordered by today, tomorrow, etc, but I'd like to put the date in brackets next to each one.
I thought it might be a case of (mock code):
$today = date("l");
$todays_date = date("j M");
if (day == $today) {
$date = $todays_date;
}
else if (day == $today + 1) {
$date = $todays_date + 1;
}
else if (day == $today + 2) {
$date = $todays_date + 2;
}
etc...
But I'm not so sure. It'd be ideal if I could just have the date in the database, but this seems to go against the grain of what MySQL is about.
Also, I'd like to ideally format the date as: 11 Jun.
EDIT
Presumably it's also got to fit into my while loop somehow:
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array($row[0]);
echo "<option>" . $row[0] . "</option>";
}
}
You can use strtotime?
echo "Today: ".date("j M");
echo "Tomorrow: ".date("j M", strotime("+1 day"));
You can use strtotime:
echo strtotime("+1 day");
I have an events calender that displays events in the order of start date. Everything works great but there is one issue. Events that occur on today's date don't display. I believe that my "if" statement to remove events after they have passed is the issue.
<? while($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$title = $row['title'];
$text = $row['text'];
$image_url = $row['image_url'];
$start_date = date('F d, Y', strtotime($row['start_date']));
$start_hour = $row['start_hour'];
$start_minute = $row['start_minute'];
$start_am_pm = $row['start_am_pm'];
$end_date = date('F d, Y', strtotime($row['end_date']));
$end_hour = $row['end_hour'];
$end_minute = $row['end_minute'];
$end_am_pm = $row['end_am_pm'];
$tba = $row['tba'];
if(strtotime($row['end_date']) > date('U')) {
?>
First of all, I just want to point out that:
date('U') == time()
So you can use time instead of date.
Now for your problem. If the end_date of your event is set to today, it's probably at the beginning of the day (i.e.: 2010-11-18 00:00:00). That's probably why your conditional does not work, because now is past midnight, the current date/time is greater than the end_date.
Try this:
if (strtotime($row['end_date']) == strtotime('TODAY')) {
// event is today
}
I'm guessing that $row['end_date'] only returns the date not the time. So strtotime($row['end_date']) will = 12:00AM and date('U') will equal return the current time.
So if you ran this at 12:01AM on the date of $row['end_date'], you'd have the comparison of
if("12:00AM today" > "12:01AM today") {
Try
if (strtotime($row['end_date']) >= strtotime(date('Y-m-d'))) {