Skip to main content

Posts

Showing posts from January, 2023

How to get the first and last day of the month in PHP

In this tutorial we'll cover how to get the first and the last day of the current month. This is particularly helpful when you want to query a database to find dates that fall within a specific month. These could be birthdays, anniversaries or just general reminders. For the sake of this tutorial, please note that the date at the time of writing is 2023-01-16. Get the first day of the current month $first_day_of_month = date('Y-m-01'); echo $first_day_of_month; The above code will output:   2023-01-01 Get the first day of any month If you would like to get the first date of any month, use the strtotime function and pass the date as a string as shown below: $first_day_of_month = date('Y-m-01', strtotime('next month')); echo $first_day_of_month; The above code will output:   2023-02-01 Get the last day of the month $last_day_of_month = date('Y-m-t'); echo $last_day_of_month; The above code will output:   2023-01-31 I hope you found this helpfu

How to display the current year in php

There are many instances where you might want to display the current year on your website. This may include copyright notices, founding dates, user sign up dates and more. In this tutorial we will cover how to display the current year using the date function in php. The date function can be used to get the current date and this can be used in combination with php's echo function as shown below:   echo date("Y"); This will output the current year. At the time of writing this article, that's 2023.

How to truncate a foreign key constrained table

The truncate function in SQL deletes all table data. However when your database has foreign key constraints this will give an error. In this tutorial, we'll cover how to TRUNCATE a table with foreign keys.  Please note that this is not advisable if your website is already in production, so ONLY do it if you're sure it will not compromise the integrity of your data. If you're using phpMyAdmin simply uncheck/disable the ' Enable foreign key checks ' checkbox as shown below: Alternatively, you could use the following SQL command. SET FOREIGN_KEY_CHECKS = 0; TRUNCATE my_table_name; SET FOREIGN_KEY_CHECKS = 1; I hope you found this helpful

Mysqli_fetch_assoc not returning result [Solved]

So I recently came across this scenario where I needed to retrieve data using the mysqli_fetch_assoc() function but was not getting any output. My code was running and it wasn't giving me any errors. I tried using mysqli_num_rows to check if there was a result and it returned 1, meaning there was a result. So if you're in the same situation, I hope this will help. I had the following code: $query = "SELECT id FROM users WHERE id = $user_id"; $result = mysqli_query($con, $query); $row = mysqli_fetch_assoc($result); echo $row['username']; After hours of cracking my head, going through my code and countless Google searches I finally realized my mistake. If you take a closer look at the code above, you will realise that I am only selecting the id column from the users table. The result will thus be a table with a single column- id. So if I check whether the query returned a result, the answer will be yes. However that result does not contain the 'userna

How to calculate a percentage in php

In this tutorial we'll cover how to calculate a percentage using php and display the result with some decimal points. In the example below, we have two numbers. One is our base upon which our percentage will be calculated and the other is the value to determine the percentage. We will be using the number_format() function to determine how many decimal points our result will have, as well as to set the separator. $possible_mark = 100; $my_mark = 78; $percentage_achieved = ($my_mark / $possible_mark) * (100); $percentage_achieved = number_format((float)$percentage_achieved, 2, '.', ''); echo $percentage_achieved . "%"; The output from the above code will will be: 78.00%

Php date: How to get the first and last day of the week

In this tutorial, I'll show you how to get the first and last day of the week in php. By default, in PHP the week runs from Monday to Sunday. However we'll cover two methods to get the first and last day for both those who consider Sunday or Monday as their first day of the week. We will be using 2 functions to achieve this: date() strtotime() We will use a combination of these two functions and store the result in a variable.  How to get the first day of the week If you want to get Sunday, use this method: $firstday = date('l Y-m-d', strtotime("sunday -1 week")); echo $firstday; If you want to get the date for Monday, this current week, use this method: $monday = date('l Y-m-d', strtotime("monday -1 week ")); echo $monday; How to get the last day of the week The following code will give you Saturday as the last day of the current week. $lastday = date('l Y-m-d', strtotime("saturday 0 week")); echo $last