how to add manual value to date() function [duplicate] - php

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 8 years ago.
I m in need of adding a defined value to the date() function.
I just want to add 2 more days with the date() function.
suppose date("Y-m-d H:i:s"); //2014-06-12 05:38:50
I just want to add 2 more days with the day value which is 12 on above example.
can anyone help ?

use php DateTime Object like :
$date = new DateTime();
$date->add(new DateInterval('P2D'));
echo $date->format('Y-m-d H:i:s') . "\n";

Related

How To Add +6 Months in current date [duplicate]

This question already has answers here:
Add six months in php
(5 answers)
Closed 6 years ago.
i want to add 6 months in current date. i have used the following date format.
$current_time = date('Y-m-d');
i have proceed with this method shown below but an error is occured.
'A non well formed numeric value encountered'.
and print this value
[expiring_plan_date] => 1970-01-02
$data['PlanPayment']['expiring_plan_date'] = date
(
"Y-m-d",
strtotime("+6 month", $current_time)
);
please help..
Remove comma from strtotime
$data['PlanPayment']['expiring_plan_date'] = date("Y-m-d", strtotime("+6 month $current_time));

Convert date format from 22/08/2015 20:36 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Good afternoon!
I have a simple question.
I have a date format in PHP
$ originalDate = "22/08/2015 20:36";
How to translate
2015-08-20 00:00:00
?
Try this:
<?php
$originalDate = "22/08/2015 20:36";
$date = DateTime::createFromFormat('d/m/Y H:i', $originalDate);
echo $date->format('Y-m-d H:i:s');
?>

Formatting date from dd-mm-yyyy to yyyy-mm-dd in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a string mm-dd-yyyy which i get from a form, which i want to store it in the database of the data-type DATE (yyyy-mm-dd).
How do i format the string and save it in the database ?
$new_format = date("Y-m-d", strtotime('04-28-2012'));
or
$date = new DateTime('04-28-2012');
$new_format = $date->format('Y-m-d');
or in PHP 5.5+
$new_format = (new DateTime('04-28-2012'))->format('Y-m-d');
Try
$date = DateTime::createFromFormat("m-d-Y", '02-15-2012');
echo $date->format('Y-m-d H:i:s') , "\n";
Output
2012-02-15 23:54:52

Formatting a date to a specific format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I have the following data available : 01/02/2014
What I need to format to is: 2014-01-02 00:00:00
What is the best way to do this in PHP?
$dt = DateTime::createFromFormat('m/d/Y', '01/02/2014');
echo $dt->format('Y-m-d 00:00:00');
See it in action
$val="01/02/2014";
echo date("Y-m-d H:i:s", strtotime($val));
Demo
First of all you should try it yourself.
you should study php date format documentation : http://www.php.net/manual/en/datetime.formats.date.php
Live demo:
https://eval.in/93149
$d = "01/02/2014";
echo date('Y-m-d H:i:s',strtotime($d));
OUTPUT:
2014-01-02 00:00:00
By using PHP date function
$date = "1st April 2000";
OR
$date = "01/04/2000";
echo date("Y-m-d H:i:s",strtotime($date));

php turning number (hours) to php date and time format [duplicate]

This question already has answers here:
PHP turn PM and AM into 24 hour
(3 answers)
Closed 8 years ago.
If I have hours in the following format :
2:30 am how can I turn it into :
14:30:00
and if I got the date 2013-07-17 and the time 14:30:00 how can I turn it to a datatimestamp ?
for example this will produce : 1374053400
thanks.
am is before noon, not after..
This is what I would do:
$timeString = '2:30 am';
$dateString = '2013-07-17';
$timeZone = new DateTimeZone('Europe/London');
$date = new DateTime($dateString . ' ' . $timeString, $timeZone);
echo $date->format('H:i:s'); // 02:30:00
echo $date->getTimestamp(); // the timestamp as int
Have you tried to search in this site? :)
The second question has a really-similar one here:
How to convert date to timestamp in PHP?
As for the first one, the answer is in the official php documentation:
http://php.net/manual/es/function.date.php

Categories