What Is The Method To Obtain The Current Date And Time In PHP?

To get the current date and time in PHP, you can use the built-in date() function. The date() function formats a local date and time based on the specified format:

$currentDateTime = date('Y-m-d H:i:s');
echo $currentDateTime;

This will output the current date and time in the format YYYY-MM-DD HH:MM:SS, where YYYY represents the year, MM represents the month, DD represents the day, HH represents the hour, MM represents the minutes, and SS represents the seconds.

You can also provide a specific timezone as the second parameter to the date() function to get the date and time in that timezone:

$currentDateTime = date('Y-m-d H:i:s', strtotime('now'), 'America/New_York');
echo $currentDateTime;

In this example, the current date and time will be displayed in Eastern Standard Time (EST) for the ‘America/New_York’ timezone.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!