CategoryEssentials

Converting string to Date and DateTime

C

solution 1. Use strtotime() on your first date then date('Y-m-d') to convert it back: $time = strtotime('10/16/2003'); $newformat = date('Y-m-d',$time); echo $newformat; // 2003-10-16 Note: There is a difference between using forward slash / and hyphen - in the strtotime() function. solution 2 Observe the difference between m/d/Y and m-d-Y formats. PHP considers / to mean m/d/Y...

Correctly determine if the date string is a valid date in that format

C

You will find the solution to the problem statement: take a string such as 2013-13-01 and tell if it is a valid date or not for the format yyyy-mm-dd? Solution 1 You can use DateTime::createFromFormat() for this purpose: function validateDate($date, $format = 'Y-m-d') { $d = DateTime::createFromFormat($format, $date); // The Y ( 4 digits year ) returns TRUE for...

How do I use PHP to get the current year?

H

Use either date or strftime. In this case, it doesn’t matter as a year is a year, no matter what (unless there’s a locale that formats the year differently).

Example

<?php echo date("Y"); ?>

Solution 2

$now = new DateTime();
$year = $now->format("Y");

Using class member access on instantiation (php>=5.4):

$year = (new DateTime)->format("Y");

Convert a date format in PHP

C

In this solution, you will learn a simple way to convert one date format into another date format in PHP. The second parameter  date) needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can’t recognize. You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn’t recognize the y-m-d-h-i-s format...

convert time stamp to time ago in php?

c

You should use the DateTime class to get the difference between 2 times, ie; $time1 = new DateTime('2014-10-06 09:00:59'); $now = new DateTime(); $interval = $time1->diff($now,true); and then use that difference (which is a DateInterval object, $interval) to find the smallest time difference like this; if ($interval->y) echo $interval->y . ' years'; elseif ($interval...

Get the current date, given a timezone in PHP?

G

The other answers set the timezone for all dates in your system. This doesn’t always work well if you want to support multiple timezones for your users. GET THE CURRENT DATE, GIVEN A TIMEZONE IN PHP Here’s the short version: <?php $date = new DateTime("now", new DateTimeZone('America/New_York') ); echo $date->format('Y-m-d H:i:s'); Works in PHP >= 5...

HOW DO I GET THE CURRENT DATE AND TIME IN PHP?

H

This article will teach important and common problems related to PHP date and time functions. Learning these solutions will help speed up PHP programming and improve logic while working with date and time functions. Date and time problems The time would go by your server time An easy workaround for this is to set the timezone by using manually date_default_timezone_set before...

Create MySQL Database in PHP

C

In the previous tutorial, we learned to establish a connection between PHP and MySQL using MySQLi and PDO. After making the connection, the very next step is to create a database. In this article, you will learn how to create a MySQL database in PHP. Create New Database using MySQLi (object-oriented) PHP provides CREATE DATABASE statement to the new database. In the following example, we will...

Connect to a MySQL Database

C

Moving a step ahead towards MySQL with PHP, we will learn how to connect to a MySQL database in PHP. There are two methods in PHP to make a connection with MySQL. MySQLi extensionPHP Data Objects (PDO) MySQLi is an improved version of MySQL which was used in the earlier versions of PHP. Which is better MySQLi or PDO? The answer depends on the scenario. Both of these methods provide an easy method...

Tutor Network

Learn PHP from A to Z