Extract the correct month from timestamp with PHP - php

got stuck in extracting the correct month from timestamp...Here's my Code
$sql = "SELECT id,notice,description,DAY( `posted_date` ) AS DAY, MONTH(`posted_date` ) AS MONTH, YEAR( `posted_date`) AS YEAR, enable FROM n_notice";
$result=mysqli_query($con,$sql);
while ($run = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$notice = $run['notice'];
$description = $run['description'];
$day =$run['DAY'];
//$month=$run['MONTH'];
$month=date("M",$run['MONTH']);
$year=$run['YEAR'];
?>
<div class="media">
<div class="media-left media-middle">
<div id="date">
<div class="mon"><?php echo $month;?></div>
<div class="day"><?php echo $day;?></div>
<div class="year"><?php echo $year;?></div>
</div>
</div>
<div class="media-body">
<h5 class="media-heading"><?php echo $notice; ?></h5>
<?php echo $description; ?>
</div>
</div>
<?php
}
// Free result set
mysqli_free_result($result);
?>
</div>
DB timestamp column
How to get the correct month in "M" format? All the Months are shown as "Jan"!!!

$month=date("M",$run['MONTH']);
Try to change "M" to "F".
F - A full textual representation of a month (January through December)

Related

Pick Up and Drop Dates not working correctly

so im currently working on a car rental and so far i have made reservations for certain cars.
Now what im trying to do is whenever someone searches for an available car , the cars that are not taken on these dates should show up .
I have stored pickup date and drop date on reservation table on database.
Car Database Start and End Rental Dates : https://i.stack.imgur.com/9mLTt.png
Car Showing when its not suppost to
https://i.stack.imgur.com/OiNRj.png
https://i.stack.imgur.com/5KMxP.png
Cars showing correctly https://i.stack.imgur.com/67oc6.png
So in other words if you are not able to get whats wrong i will try to make it as clear as possible.
I have 4 dates which are named as below
DB - StartDate = '2023-02-06'DB - EndDate = '2023-02-08'
DB - Selected_car = '12' is the car ID Then we have
StartDate = '2023-02-06' - This is the date inputed by the client
EndDate = '2023-02-07' - As Show in the images above
This search query would put the car as available which should not because the car is taken from date 06 till 08 also if you search till 09 as end date , the car shows available which should not because starting date is 06 and end is 08 which means the car is taken in these dates. Below you'll find the code i did for the search.php
<?php
if(isset($_POST['submit']))
{
$startDate = $_POST['pickupDate'];
$returnDate = $_POST['returnDate'];
$startDate = new DateTime($startDate);
$returnDate = new DateTime($returnDate);
$startDate = $startDate->format("Y-m-d ");
$returnDate = $returnDate->format("Y-m-d ");
if(empty($startDate))
{
$_SESSION['ErrorMessage'] = "Pickup Date can't be empty";
TakeTo("index.php");
}
elseif(empty($returnDate))
{
$_SESSION['ErrorMessage'] = "Return Date can't be empty";
TakeTo("index.php");
}
elseif($startDate == $returnDate)
{
$_SESSION['ErrorMessage'] = "Cars can only be rented for 1 or more days";
TakeTo("index.php");
}
elseif($returnDate <= $startDate)
{
$_SESSION['ErrorMessage'] = "Return date cannot be before Pickup Date";
TakeTo("index.php");
}
echo "<h3 class='text-center mt-2 bg-light'>Pickup Date : $startDate & Return Date : $returnDate <br> </h3>";
$carIDArray = array();
$data = [
'dateStart' => $startDate,
'dateEnd'=> $returnDate,
];
$sql = "SELECT
reservations.selected_car
FROM
reservations
WHERE
reservations.start_date >= :dateStart
AND
reservations.end_date <= :dateEnd
GROUP BY
reservations.selected_car"
;
$stmt = $con->prepare($sql);
$execute=$stmt->execute($data);
if($execute)
{
$results = $stmt->rowcount();
if($results >= 1)
{
while($resInfo = $stmt->fetch())
{
$carID = $resInfo['selected_car'];
$carIDArray[] = $carID;
}
$carIds = implode(",", $carIDArray);
$sql2 = "SELECT
cars.*
FROM
cars
WHERE
!FIND_IN_SET(cars.id,'$carIds') ORDER BY id DESC";
$stmt2 = $con->query($sql2);
while($carInfo = $stmt2->fetch())
{
$AllIds = $carInfo['id'];
$carName = $carInfo['car_name'];
$carEngine = $carInfo['engine'];
$carYear = $carInfo['production_year'];
$carSpend = $carInfo['100km'];
$carCapacity = $carInfo['full_tank'];
$carDoors = $carInfo['doors'];
$carImage = $carInfo['picture'];
$carTransmition = $carInfo['transmition'];
$carPrice = $carInfo['dailyprice'];
$carPetrol = $carInfo['fuel_type'];
?>
<!--Cars Display All-->
<div class="container mt-3">
<div class="container-fluid">
<div class="card mb-3 all-cars">
<div class="row g-0">
<div class="col-md-4 col-sm-6">
<img height="250" width="100%" src="carimages/<?php echo $carImage; ?>" class="rounded-star" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h3><?php echo $carName; ?></h3>
<hr class="bg-black">
<div class="d-block">
<table>
<tbody>
<tr>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gear"></i> <?php echo $carTransmition; ?> </span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-car-side"></i> <?php echo $carDoors; ?> Doors</span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gas-pump"></i> <?php echo $carSpend; ?>L/100km </span></td>
</tr>
<tr><td><br></td></tr>
<tr>
<td> <span class="m-3 bold-text "><i class="fa-solid fa-calendar-day"></i> <?php echo $carYear; ?></span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gas-pump"></i> <?php echo $carCapacity; ?>L </span></td>
<td> <span class="m-3 bold-text"><i class="fa-solid fa-gas-pump"></i> <?php echo $carPetrol; ?> </span></td>
</tr>
</tbody>
</table>
<h5 class="text-end top-50 m-3 bold-text"><strong ><?php echo $carPrice; ?>€</strong>/day</h5>
Choose
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
else

Conditional statement not giving desired output in PHP 7.4

I have a PHP script that queries the database and produces results based on the condition. In PHP 7.1 all worked well. But the server the website is hosted has requested I change the server PHP language to 7.4.
After the change I noticed the issue of the desired results not appearing as it should.
Below are my codes.
<?Php if (($checkday != "Sunday")&&($phdate == "")) {
$davsquery = "SELECT davsID, davs, davsslots FROM dailyavailableslotsus WHERE YEAR(davs)=? AND MONTH(davs)=? AND DAY(davs)=?";
$davsstmt = $connQlife->prepare($davsquery);
$davsstmt->bind_param('sss', $year, $month, $day);
$davsstmt->execute();
$davsstmt->store_result();
$davsstmt->bind_result($davsID, $davs, $davsslots);
$davsstmt->fetch();
$davsstmt->close();
if (($davsslots!="")&&($totalnumrows!="")) {
$availableslots = $davsslots - $totalnumrows;
}
if (($davsslots!="")&&($totalnumrows=="")) {
$availableslots = $davsslots;
}
if (($totalnumrows=="")&&($davsslots=="")) {
$availableslots = $defaultusimslots;
}
if (($checkday == "Saturday")&&($davsslots=="")) {
$availableslots = $usimslots20 - $totalnumrows;
}
if (($checkday == "Saturday")&&($davsslots!="")) {
$availableslots = $davsslots - $totalnumrows;
}
?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<?Php if ($day > $curday){ ?>
<div class="calendartextcont">
<?Php if ($availableslots > 5){ ?>
<div class="calendartextg">Available slots: <?Php echo $availableslots; ?></div>
<?php } ?>
<?Php if (($availableslots >= 1)&&($availableslots <= 5)) { ?>
<div class="calendartexto">Available slots: <?Php echo $availableslots; ?></div>
<?php } ?>
<?Php if ($availableslots <= 0){ ?>
<div class="calendartextr">No Slots Available!</div>
<?php } ?>
</div>
<?Php if ($availableslots >= 1){ ?>
<label class="calendarcheckbox">Select
<input type="checkbox" name="screening" id="screening" value="<?Php if ($screeningdate=="") {echo date('Y-m-d', mktime(0,0,0,$month,$day,$year)); }else{ echo $screeningdate; } ?>">
<span class="checkmark"></span>
</label>
<?php } ?>
<?php } else { ?>
<div class="calendartextcont">
<div class="calendartextr">Booking Closed!</div>
</div>
<?php } ?>
</div>
<?Php } ?>
<?Php if ($checkday == "Sunday") { ?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr">Closed On Sundays!</div>
</div>
</div>
<?Php } ?>
<?Php if ($phdate != ""){ ?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr"><?Php echo $phdetail; ?></div>
</div>
</div>
<?Php } ?>
What changed in PHP 7.4 that may be affecting the script? Or is there a better way to rewrite this that produces the same results but in code practice favorable to PHP 7.4
To put this into context, this is a subset of a larger script that loops through the days of each month and shows how many slots are available for someone who wants to make a booking for that day.
There is a default value of 30 slots per day. There is also a table that takes entries of a particular number of slots for any particular day specified. There is also a table that takes public holidays. The script is supposed to check if there is a manual entry of slots for a particular day and if there is, check the number of slots available for that day and subtract from the manual entry to determine how many slots are left. If not manual entry, the default slots is then used for that day.
On PHP 7.4, if I specify a manual entry of say 10 slots for a particular day, the output when the entire script is run is that all days of the current month now shows 10 as the slots available rather than 30 and only show 10 for the particular day specified.
The other issue I noticed is that, if I specify a public holiday, all days after that day also show as the public holiday.
But when I switch back to PHP 7.1 all works perfectly.

Bootstrap panel sorting by dates with php

I'm building php mysql app for job interviews, and I want to have for admin filer with bootstrap panels which are sorted by date in the same color column.
$db = connectPDO();
$query = ("SELECT razgovori.*, djelatnik.ime, djelatnik.prezime
FROM razgovori INNER JOIN djelatnik
ON djelatnik.id = razgovori.insert_by
ORDER BY datum_raz ASC");
$stmt = $db->prepare($query);
$stmt->execute();
$count = $stmt->rowCount();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$date = strtotime($row['datum_raz']);
$twoDaysAgoStart = strtotime('-2 day 00:00:00');
$yesterdayEnd = strtotime('yesterday 23:59:59');
$todayStart = strtotime('today 00:00:00');
$todayEnd = strtotime('today 23:59:59');
$tomorrowStart = strtotime('tomorrow 00:00:00');
$twoDaysAheadEnd = strtotime('+2 day 23:59:59');
<div class="container">
<div class="row">
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)): ?>
<?php $date = strtotime($row['datum_raz']); ?>
<?php if ($twoDaysAgoStart < $date && $yesterdayEnd > $date): ?>
<!--Panel info if past time Job interviews-->
<div class="col-md-4 pull-left">
<div class="panel panel-info">
<div class="panel-heading"><h3 class="panel-title"><?php echo
datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
<div class="panel-body">
<div class="col-md-9">
<h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php
echo $row['rime'].' '.$row['rprezime']; ?></a></h5>
<?php echo $row['kontakt_br']; ?>
</div>
<div class="col-md-3">
<span class="label label-default pull-right"></h5><?php echo
$row['ime'].' '.$row['prezime']; ?></span>
</div>
</div>
</div>
</div>
<!--//Panel-->
</tr>
<tr>
<!--Panel primary if Job interview is today-->
<?php elseif ($todayStart <= $date && $todayEnd >= $date): ?>
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading"><h3 class="panel-title"><?php echo
datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
<div class="panel-body">
<div class="col-md-9">
<h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php echo
$row['rime'].' '.$row['rprezime']; ?></a></h5>
<?php echo $row['kontakt_br']; ?>
</div>
<div class="col-md-3">
<span class="label label-default pull-right"></h5><?php echo
$row['ime'].' '.$row['prezime']; ?></span>
</div>
</div>
</div>
</div>
</tr>
<tr>
<!--Panel warning if Job interview is tomorrow-->
<?php elseif ($tomorrowStart <= $date && $twoDaysAheadEnd >= $date): ?>
<div class="col-md-4 pull-right">
<div class="panel panel-warning">
<div class="panel-heading"><h3 class="panel-title"><?php echo
datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
<div class="panel-body">
<div class="col-md-9">
<h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php
echo $row['rime'].' '.$row['rprezime']; ?></a></h5>
<?php echo $row['kontakt_br']; ?>
</div>
<div class="col-md-3">
<span class="label label-default pull-right"></h5><?php echo
$row['ime'].' '.$row['prezime']; ?></span>
</div>
</div>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div><!--/row-->
</div><!--/container-->
I want to have sorted like thiswith Main Panal or without
I get this:enter image description here
Because stackoverflow red message constantly asking me for more details, i can't put my all php code
You can do this in your MySQL query.
Example:
SELECT * FROM t1
ORDER BY key_part1, key_part2;
SELECT * FROM t1
WHERE key_part1 = constant
ORDER BY key_part2;
SELECT * FROM t1
ORDER BY key_part1 DESC, key_part2 DESC;
SELECT * FROM t1
WHERE key_part1 = 1
ORDER BY key_part1 DESC, key_part2 DESC;
SELECT * FROM t1
WHERE key_part1 > constant
ORDER BY key_part1 ASC;
SELECT * FROM t1
WHERE key_part1 < constant
ORDER BY key_part1 DESC;
SELECT * FROM t1
WHERE key_part1 = constant1 AND key_part2 > constant2
ORDER BY key_part2;
More information can be found here: https://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html or https://www.w3schools.com/sql/sql_orderby.asp
If you want to do this inside PHP you can use sort:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}
?>
The above example will output:
fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] =
orange
More information about PHP sort():
http://php.net/manual/en/function.sort.php

skip same dates in foreach loop

this is a text file content which will be chatting database
you:2016-05-02 11:41:53 Hi
Muhammad:2016-05-02 11:42:41 Hi
you:2016-05-02 11:43:33 How are you ?
Muhammad:2016-05-02 14:44:56 I'm fine!
this is the code to loop to get content
<?php
$chat = file("members/cdn/1/chats/9188.txt");
foreach($chat as $line){
$name = strchr($line,":",true);
$message = explode(' ', substr(strchr($line,":"),1), 3);
if(some thing){
?>
<div>
<!-- here i want to skip the same dates -->
<?=$message[0];?>
</div>
<?php
}
?>
<div class="container">
<div class="arrow">
<div class="outer"></div>
<div class="inner"></div>
</div>
<div class="message-body">
<p><?=$message[2];?></p>
<p class="message_time"><?=date("g:i a", strtotime($message[1]));?></p>
</div>
</div>
<div class="spacer"></div>
<?php
}
?>
I want the date of the message appear one time above of messages in the same date
Simply remember that date you last used and then compare it to the one in $message[0]
<?php
$lastDate = NULL;
$chat = file("members/cdn/1/chats/9188.txt");
foreach($chat as $line) :
$name = strchr($line,":",true);
$message = explode(' ', substr(strchr($line,":"),1), 3);
if($lastDate != $message[0]) :
$lastDate = $message[0];
?>
<div><?=$message[0];?></div>
<?php
endif;
?>
<div class="container">
<div class="arrow">
<div class="outer"></div>
<div class="inner"></div>
</div>
<div class="message-body">
<p><?=$message[2];?></p>
<p class="message_time"><?=date("g:i a", strtotime($message[1]));?></p>
</div>
</div>
<div class="spacer"></div>
<?php
endforeach;
?>
Try this:
$prevDate[] = array();
foreach($chat as $line){
$name = strchr($line,":",true);
$message = explode(' ', substr(strchr($line,":"),1), 3);
if(some thing){
?>
<div>
<!-- here i want to skip the same dates -->
<?php
if(!in_array($message[0],$prevDate)) { // check if date exist in array - means displayed previously or not
echo $message[0];
$prevDate = $message[0]; // store date in array so that next time you can check whether it has been already displayed or not
}
?>
</div>
<?php
}
?>
<div class="container">
<div class="arrow">
<div class="outer"></div>
<div class="inner"></div>
</div>
<div class="message-body">
<p><?=$message[2];?></p>
<p class="message_time"><?=date("g:i a", strtotime($message[1]));?></p>
</div>
</div>
<div class="spacer"></div>
<?php
}
First, you can use list assignment to get the components split out into separate vars:
list($user,$date,$time,$message) = explode(' ', substr(strchr($line,":"),1), 4);
Then you can use a simple comparison to see if the date is new:
if ($date != $last_date) {
$last_date = $date;
?><div><?=$date?></div><?php
}
You should declare $last_date before the loop, but you can leave its value undefined.

How to format date in PHP from phpMyAdmin database

I can't figure out how to format the timestamp on my database so I figured I should use my PHP code, but still cannot figure it out.
PHP CODE:
function filldiv() {
$loopResult .= '';
$events = mysql_query('SELECT * FROM a3825952_blog.Blog ORDER BY DATE DESC');
while($row = mysql_fetch_array($events)) {
$loopResult .= '
<div class="blogbox show">
<div class="blogtitle">'.$row['TITLE'].'</div>
<div class="blogdate">'.$row['DATE'].'</div>
<div class="blogcontent">'.$row['CONTENT'].'</div>
<div class="blogimage"> <img src="../'.$row['IMAGE'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEB'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEC'].'"/></div>
<div class="showHide"></div>
</div>
';
}
echo $loopResult;
Thanks for every ones suggestions on reading the documentation. For some odd reason I just didn't think of it. I was able to figure it out and posted the solution below. Again, thanks!
First I had to set the rows date to a variable, then format the date in the loop.
function filldiv() {
$loopResult .= '';
$events = mysql_query('SELECT * FROM database.table ORDER BY DATE DESC');
while($row = mysql_fetch_array($events)) {
$date = date_create($row['DATE']);
$loopResult .= '
<div class="blogbox show">
<div class="blogtitle">'.$row['TITLE'].'</div>
<div class="blogdate">'.date_format($date, 'l, F j, Y').'</div>
<div class="blogcontent">'.$row['CONTENT'].'</div>
<div class="blogimage"> <img src="../'.$row['IMAGE'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEB'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEC'].'"/></div>
<div class="showHide"></div>
</div>
';
}
echo $loopResult;

Categories