Popular PHP library for working with dates and times The 100th post on PHP. Wow. Okay, enough patting myself on the back. Let’s get to Carbon. What is Carbon? Remember when we covered the DateTime object? It was post 98. Well, Carbon is a PHP dependency that extends the DateTime class. It has a lot more simplified interface and is extremely simple to use. https://blog.devgenius.io/php-p98-datetime-object-1191e4b457c1 https://blog.devgenius.io/php-p99-dependency-management-intro-fe42877964d4 Installing the Carbon Dependency Before you can install Carbon, you’ll need to make sure that you have composer installed. Composer is a dependency manager and was the topic of the last article. Once you have composer installed, run the following command. composer require nesbot/carbon
Tag: PHP Programming
A cornerstone of modern software development practices We’ve made it to almost 100 articles without discussing dependencies in PHP. We ended the previous article on the DateTime built in class. We used it to perform some date and time operations. Next on the list is Carbon. Carbon is a dependency that’s installable using composer. Once installed, we can use it like any other built in class. Carbon actually inherits from the DateTime class and expands on it. But, before we get into that, I wanted to briefly touch on dependencies in PHP. You might have heard on npm if you’ve ever worked on any front-end application that uses JavaScript. npm is a
The DateTime object in PHP simplifies date and time manipulation In the previous article, we looked at some of the most frequently used date and time functions within PHP. What we haven’t covered is time manipulation. How do you add 2 weeks to a current date-time or how many days are between two dates? That’s where the DateTime object shines. https://blog.devgenius.io/php-p97-date-and-time-introduction-8174c21a447e The DateTime Object DateTime is a class that’s built into PHP. That means that you can instantiate it without doing anything special. <?php $datetime = new DateTime(); var_dump($datetime); /app/95 DateTime Object/index.php:3: object(DateTime)[1] public ‘date’ => string ‘2022-11-20 19:33:12.650004’ (length=26) public
calculate intervals & perform date arithmetic in PHP Here’s another one of those things that people find scary: date and time. Why don’t we demystify this topic as well. Let’s look at the traditional way of doing this first and then the Carbon way in the next couple of articles. We’ll also look at time addition and subtraction in the next article with the DateTime object. The easiest way to get into this is with examples. We’ll be tackling the following date and time functions: getdate() localtime() date() strtotime() mktime() checkdate() getdate() The getdate() function returns an array with the following values
Primary and foreign keys: The backbone of relational databases We spoke about normalization but never fully covered primary and foreign keys. It’s not something that’s unique to MySQL, but it’s something that you hear quite frequently: primary key and foreign key. We have already looked at an example of this in the previous MySQL article, but I wanted to elaborate on it a little further. https://blog.devgenius.io/php-p93-mysql-normalization-7e9a9809311e What is a Primary Key in SQL? It’s just a column or combination of columns inside of your table that is unique for each of the entries within that table. A quick example is
Cookies remember user preferences and login status I’m starting to run out of topics to write about so this might be my last one on PHP before I transition to Laravel. Exciting and sad at the same time. Once you get past this topic, anything else that you want to learn PHP related is going to be pretty trivial. Have fun exploring additional features, finding unique ways to code, standardizing your code, and progressing into frameworks. https://blog.devgenius.io/php-p94-sessions-23e106ad374f What is a Cookie? A cookie is small text-file with user identifiable information that’s set on the user’s computer by the server. The cookie
Sessions enable personalized and stateful experiences What is a session? It’s a way to store data on the server without using the database. When a user goes from page to page, each request generates a new set of variables that will be used to help generate the pages that the user needs to see. The variables are temporary, meaning that once the user visits another page, those variables in your code are gone. In order to have data persistence so far, we’ve used the database. If you need permanent data persistence, you should absolutely continue using the database. But what
Normalization in MySQL streamlines database organization We’ll touch on one last topic and call it a day with MySQL. The last topic is normalization. What does it mean to normalize a database? Let’s say that you have a table that stores your customers and one of the fields there is customer_type, such as direct or distributor, those two words are going to be entered thousands of times unnecessarily. Why not have a separate table for them and just specify which number they represent? For example, if id of 1 is set to distributor, then if we look at a customer’s customer_type and see that the value is set to 1, then we’ll
Controllers bridge the gap between users and databases We’ve made it past the basic MySQL functionality. I do want to cover normalization, and will do that in the next article, but I believe we need to introduce Controllers. It will simplify the concept when you start learning about Laravel, since it is an MVC framework. https://blog.devgenius.io/php-p91-mysql-crud-1988f82072f5 What is an MVC Framework? MVC stands for Model, View, Controller. I’m not going to go into strict definitions like something, something, business logic. Let’s speak human-talk. Model — The Class that interacts with the database. In our previous examples, this is the Author class. View
From Creation to Deletion: MySQL’s Complete Data Toolkit What are CRUD operations? Create, Read, Update, Delete. That’s what we have created over the last few articles. The last piece of the puzzle is to combine them into one file that knows how to manage our authors table. If you’re jumping straight into this article, I recommend that you read the earlier articles leading up to this one first. You don’t need to, but it’ll help. MySQL Introduction MySQL DB Connection MySQL Tables MySQL Insert MySQL Insert with Prepared Statements MySQL Insert Multiple Records with Prepared Statements MySQL Select MySQL Update (PUT vs