Hướng dẫn sub year php

I've got a date in this format:

2009-01-01

How do I return the same date but 1 year earlier?

elitalon

9,0319 gold badges48 silver badges86 bronze badges

asked Jan 2, 2010 at 1:43

2

You can use strtotime:

$date = strtotime('2010-01-01 -1 year');

The strtotime function returns a unix timestamp, to get a formatted string you can use date:

echo date('Y-m-d', $date); // echoes '2009-01-01'

answered Jan 2, 2010 at 1:45

0

Use strtotime() function:

  $time = strtotime("-1 year", time());
  $date = date("Y-m-d", $time);

answered Jan 2, 2010 at 1:45

AlexAlex

3,1002 gold badges21 silver badges22 bronze badges

1

Using the DateTime object...

$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');

Or using now for today

$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');

answered Sep 10, 2013 at 8:34

darrenwhdarrenwh

6305 silver badges4 bronze badges

1

an easiest way which i used and worked well

date('Y-m-d', strtotime('-1 year'));

this worked perfect.. hope this will help someone else too.. :)

answered Oct 26, 2016 at 6:58

saadksaadk

1,17512 silver badges17 bronze badges

// set your date here
$mydate = "2009-01-01";

/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));

// format and display the computed date
echo date("Y-m-d", $lastyear);

answered Jan 2, 2010 at 1:57

NirmalNirmal

9,24110 gold badges54 silver badges80 bronze badges

On my website, to check if registering people is 18 years old, I simply used the following :

$legalAge = date('Y-m-d', strtotime('-18 year'));

After, only compare the the two dates.

Hope it could help someone.

answered Dec 5, 2018 at 8:09

MelomanMeloman

3,2343 gold badges39 silver badges44 bronze badges

Although there are many acceptable answers in response to this question, I don't see any examples of the sub method using the \Datetime object: https://www.php.net/manual/en/datetime.sub.php

So, for reference, you can also use a \DateInterval to modify a \Datetime object:

$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));

echo $date->format('Y-m-d');

Which returns:

2008-01-01

For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php

answered Jan 13, 2020 at 13:09

Oliver TappinOliver Tappin

2,4511 gold badge23 silver badges42 bronze badges

You can use the following function to subtract 1 or any years from a date.

 function yearstodate($years) {

        $now = date("Y-m-d");
        $now = explode('-', $now);
        $year = $now[0];
        $month   = $now[1];
        $day  = $now[2];
        $converted_year = $year - $years;
        echo $now = $converted_year."-".$month."-".$day;

    }

$number_to_subtract = "1";
echo yearstodate($number_to_subtract);

And looking at above examples you can also use the following

$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));

answered Aug 26, 2017 at 11:37

0

Hướng dẫn sub year php

Hướng dẫn how to update php

18/04/2022 10:02 am | Luợt xem : 1626Hướng dẫn cách update PHP version lên phiên bản mới nhất giúp trang web của bạn được nâng cấp hơn trước với những vấn đề ...

Hướng dẫn sub year php

Hướng dẫn sub year php

Hướng dẫn phpdocumentor vscode

How does python interpreter choose vs code?This article discusses the helpful Python environments features available in Visual Studio Code. An environment in Python is the context in which a Python ...

Hướng dẫn sub year php

Hướng dẫn dùng text hash trong PHP

Tìm hiểu khi sử dụng các hàm băm tạo dữ liệu lưu trữ passwordKhi lưu trữ password vào CSDL thường sẽ sử dụng các hàm băm khác nhau được hỗ trợ bởi hệ ...

Hướng dẫn sub year php

How to insert image in phpmyadmin

Uploading the image/videos into the database and displaying it using PHP is the way of uploading the image into the database and fetching it from the database. Using the PHP code, the user uploads ...

Hướng dẫn sub year php

Hướng dẫn php artisan make:model

1. Tạo ModelĐể tạo model ta mở command line và gõ lệnh sau:php artisan make:model NewsHoặc php arisan make:model News --migrationTrong đó:News là tên model, các bạn có ...

Hướng dẫn sub year php

Hướng dẫn php strtr vs str_replace

First difference:An interesting example of a different behaviour between strtr and str_replace is in the comments section of the PHP Manual:

Hướng dẫn sub year php

How to format text in php?

I want to do text formatting italic of my data which is echo from database. In my database, synonyms field their are more than one name and I retrieve all in one echo. For example Calappa nucifera ...

Hướng dẫn sub year php

What are the requirements to install php?

PHP requires at least Windows 2008/Vista. Either 32-Bit or 64-bit (AKA X86 or X64. PHP does not run on Windows RT/WOA/ARM). As of PHP 7.2.0 Windows 2008 and Vista are no longer supported. PHP ...

Hướng dẫn sub year php

Hướng dẫn php base path

Trong lập trình PHP, việc lấy đường dẫn tuyệt đối, document root hay base url sẽ là 1 chút khó khăn cho những bạn mới làm quen với PHP. Vì vậy bài viết này ...

Hướng dẫn sub year php

Can you call php from javascript?

This work perfectly for me:To call a PHP function (with parameters too) you can, like a lot of people said, send a parameter opening the PHP file and from there check the value of the parameter to ...

Hướng dẫn sub year php

Hướng dẫn dùng oauth trong PHP

Để cho nhanh chóng, các bạn muốn biết Oauth2 để làm cái gì thì mời các bạn đọc bài Introduction to Oauth2 của anh Tùng D. Bài này mình sẽ giới thiệu cách sử ...

Hướng dẫn sub year php

Hướng dẫn sub year php

Hướng dẫn format number php money

(PHP 4 >= 4.3.0, PHP 5, PHP 7)money_format — Formats a number as a currency stringWarningThis function has been DEPRECATED as of PHP 7.4.0, and REMOVED as of PHP 8.0.0. Relying on this function ...

Hướng dẫn sub year php

What does the :: mean in php?

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a ...

Hướng dẫn sub year php

Is null is same as in php?

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)is_null — Finds whether a variable is nullDescriptionis_null(mixed $value): bool Parameters value The variable being evaluated. Return Values Returns true ...

Hướng dẫn sub year php

Hướng dẫn dùng affected rows trong PHP

Hàm mysqli_affected_rows() sẽ trả về số dòng bị ảnh hưởng các truy vấn SELECT, INSERT, UPDATE, REPLACE, hoặc DELETE trước đó.Bài viết này được đăng ...

Hướng dẫn sub year php

Get first date of month php

How can I find the first and last date in a month using PHP? For example, today is April 21, 2010; I want to find April 1, 2010 and April 30, 2010. S.L. Barth8,08971 gold badges50 silver badges63 ...

Hướng dẫn sub year php

Can i run php code in vs code?

PHP is a web scripting language that helps us create dynamic and interactive web pages. This guide shows you how to run a PHP file in Visual Studio Code.This tutorial is geared towards beginners and ...

Hướng dẫn sub year php

Hướng dẫn php full form

PHP code is executed on the server.Nội dung chínhWhat You Should Already KnowWhat is PHP?What is a PHP File?What Can PHP Do?Whats new in PHP 7PHP: Hypertext Preprocessor (earlier called, Personal ...

Hướng dẫn sub year php

Hướng dẫn sub year php

Hướng dẫn sub year php

Late static binding in php

PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. More precisely, late static bindings work by storing the ...

Hướng dẫn sub year php

Hướng dẫn php curl login session

Hôm nay hà minh giới thiệu với các bạn làm thế nào curl được login của môt trang web bất kỳ và xử lý được dự liệu trong ấy như thế nào. chúng ta bắt ...

Hướng dẫn sub year php

Hướng dẫn dùng list replaceall trong PHP

Cú phápCú pháp: str_replace($search, $replace, $subject);Nội dung chínhKết Quả trả về1. Tìm hiểu về Phương thức replace() Cú pháp của phương thức replace()2. Ví ...

Hướng dẫn sub year php

Hướng dẫn php webshell reverse shell

Reverse shell là gì ?Reverse shell là 1 loại session shell (ngoài ra còn có web shell, bind shell,.. ) là shell có kết nối bắt nguồn từ 1 máy chủ đóng vai trò là target ...

Hướng dẫn sub year php

Php parse json stdclass object

Im pretty new to arrays still. I need some help - I have some JSON, and Ive run it through some PHP that basically parses the JSON and decodes it as follows:stdClass Object ( [2010091907] => ...

Hướng dẫn sub year php

Hướng dẫn dùng enable ftp trong PHP

Giới thiệu PHP FTPCác chức năng FTP cung cấp cho máy khách quyền truy cập vào máy chủ tệp thông qua Giao thức truyền tệp (FTP).Các chức năng FTP được sử dụng ...

Hướng dẫn sub year php

How do i set the time zone in html?

Related to ACTION-149Now tracked by: HTML BUG 26641Contents1 Introduction2 Proposal3 Other Alternatives3.1 Acknowledgements IntroductionA gap in HTML is that date and time values cannot be ...

Hướng dẫn sub year php

Hướng dẫn rtrim php comma

Get image name from folder in phpI have been trying to figure out a way to list all files contained within a directory. Im not quite good enough with php to solve it on my own so hopefully someone ...

Hướng dẫn sub year php

Hướng dẫn dùng pushing string trong PHP

Khi lập trình php chúng ta thường phải nối các chuỗi hoặc giữa chuỗi và biến lại với nhau để phục vụ cho một phần trong chương trình. Bạn yên tâm, trong ...

Hướng dẫn sub year php

Hướng dẫn php pdo parameterized query

Chuyển đến nội dung chínhTrình duyệt này không còn được hỗ trợ nữa.Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật ...

Hướng dẫn sub year php

Hướng dẫn dùng resizeimage net trong PHP

Vietnamese (Tiếng Việt) translation by Dai Phong (you can also view the original English article) Nội dung chínhGiới thiệu Bước 1 Chuẩn bị Bước 2 Gọi Đối tượng Bước 3 ...

Hướng dẫn sub year php

Can php send asynchronous request?

Event ExtensionEvent extension is very appropriate. It is a port of Libevent library which is designed for event-driven I/O, mainly for networking.I have written a sample HTTP client that allows to ...

Hướng dẫn sub year php

Hướng dẫn dùng define errors trong PHP

Trong bài này, chúng ta sẽ tìm hiểu về cách xử lý lỗi (error) trong PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập trình Web ...

Hướng dẫn sub year php

Hướng dẫn dùng rand c trong PHP

Làm sao để sinh số ngẫu nhiên trong C/C++? Hãy cùng Techacademy đi tìm cách để khởi tạo các số ngẫu nhiên sử dụng C/C++ nhé. Mình sẽ hướng dẫn các bạn ...

Hướng dẫn sub year php

Hướng dẫn callback function in php

Bài viết được sự cho phép của tác giả Kien Dang ChungCallback là khái niệm một hàm được truyền vào một hàm khác như một tham số để nó có thể được ...

Hướng dẫn sub year php

Hướng dẫn phpcs options

PHP_CodeSniffer (PHPCS) is a tool that validates your code against a set of predefined standards and ensures that such standards are maintained across the team. This tutorial will walk you through ...

Hướng dẫn sub year php

What are the control flow statements in php?

2.5.3. while The simplest form of loop is the while statement: while (expression) statementIf the expression evaluates to true, the statement is executed and then the expression is reevaluated ...

Hướng dẫn sub year php

Hướng dẫn dùng w3schools functions trong PHP

The real power of PHP comes from its functions.PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.PHP Built-in FunctionsPHP has over 1000 built-in ...