php date not converting properly - php

I have this date in php: 31/01/2013
I'm trying to convert it using the strtotime function like so
date("Y-m-d", strtotime(31/01/2013));
but it keeps displaying as 1970-01-01. Any know why this is?

you should include it inside a string, not a continuous series of dividing numbers
date("Y-m-d", strtotime("31/01/2013"));

This will work
$date = str_replace("/", "-", "31/01/2013");
echo date("Y-m-d", strtotime($date));

Try this
$date = "31/01/2013";
$date = date("Y-m-d", strtotime($date));
Hope it will help

Try this
$date = "01/08/2013";
echo date('Y-m-d', $date);

Related

How to format string to date

I trying to format string to date. String looks like that: 20200219 and I want to format it like 2020-02-19. Can some help me with this?
You have multiple options:
Use strtotime() on your first date then date('Y-m-d') to convert it back:
$changed_date = "20200219";
echo date("Y-m-d", strtotime($changed_date ) );
$time = strtotime('03/05/2020');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2020-03-05
You need to be careful with m/d/Y and m-d-Y formats. PHP considers / to mean m/d/Y and - to mean d-m-Y. I would explicitly describe the input format in this case:
$ymd = DateTime::createFromFormat('m-d-Y', '03/05/2020')->format('Y-m-d');
Another Option:
$d = new DateTime('03/05/2020');
$timestamp = $d->getTimestamp(); // Unix timestamp
$formatted_date = $d->format('Y-m-d'); // 2020-03-05
you can do like that
$s = '20200219';
$date = strtotime($s);
echo date('Y-m-d', $date);
if it's a string you could do like this:
$date=date_create("20200219");
return date_format($date,"Y-m-d");
Hope it helped.
Try this one:-
$var = "20200219";
echo date("Y-m-d", strtotime($var) );
You can try php way:
date("Y-m-d", strtotime("20200219") );

PHP get date string with leading zeros

I want to get the current date as a string with leading zeros, means:
$date = date("Y-n-j", time());
gives me eg: 2014-9-12, but what I want is this: 2014-09-12
Here is a list of all the things you can do to format dates with date()
$date = date("Y-m-d");
Please read this: http://php.net/manual/en/datetime.format.php
<?php
$date = date("Y-m-d", time());
echo $date;
?>

Date Format Conversion in PHP 5.1.2

1.2 and need to convert a date from dd/mm/yyyy to yyyy-mm-dd
For example if the date is in format 07/08/2014, it should appear as 2014-08-07
How can this be done? I know strtotime returns unix timestamp but it doesn't seem to work with dates with Slashes (/) in it. SInce I'm using 5.1, a lot of DateTime functions are not supported in it.
Please help.
Use DateTime class, strtotime function would create issue when date less then 1901 with PHP 5.3.0
Try this way
$date = DateTime::createFromFormat('d/m/Y', "07/08/2014");
$new_date_format = $date->format('Y-m-d');
Need to pass a correct format with -(date string separation with dash) in date() try
$d = str_replace('/', '-','07/08/2014');
echo date('Y-m-d', strtotime($d)); //2014-08-07
with DateTime
$objDateTime = new DateTime($d);
echo $objDateTime->format('Y-m-d'); //2014-08-07
You can do it by date('Y-m-d',strtotime($date))
Where $date is in any format that you want to convert to YYYY-MM-DD format.
By using date() function yuo can try this
echo date('Y-d-m',strtotime('07/08/2014'));
Check the documentation for more
Method : 1 demo
$date1 = "07/08/2014";
$arr = explode("/", $date1);
$date2 = $arr[2]."-".$arr[1]."-".$arr[0];
echo $date2;
Method : 2 demo
$date1 = "07/08/2014";
list($day, $month, $year) = explode("/", $date1);
$date2 = $year."-".$month."-".$day;
echo $date2;
Method 3 : with strtotime Demo
$date1 = "07/08/2014";
$date1 = str_replace("/", "-", $date1);
$date2 = date('Y-m-d', strtotime($date1));
echo $date2;

PHP - add 1 day to date format mm-dd-yyyy

<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);
?>
This is driving me crazy and seems so simple. I'm pretty new to PHP, but I can't figure this out. The echo returns 01-01-1970.
The $date will be coming from a POST in the format m-d-Y, I need to add one day and have it as a new variable to be used later.
Do I have to convert $date to Y-m-d, add 1 day, then convert back to m-d-Y?
Would I be better off learning how to use DateTime?
there you go
$date = "04-15-2013";
$date1 = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));
echo $tomorrow;
this will output
04-16-2013
Documentation for both function
date
strtotime
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$date->modify('+1 day');
echo $date->format('m-d-Y');
See it in action
Or in PHP 5.4+
echo (DateTime::createFromFormat('m-d-Y', '04-15-2013'))->modify('+1 day')->format('m-d-Y');
reference
DateTime::createFromFormat()
$date = strtotime("+1 day");
echo date('m-d-y',$date);
use http://www.php.net/manual/en/datetime.add.php like
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');
output
2000-01-2
The format you've used is not recognized by strtotime(). Replace
$date = "04-15-2013";
by
$date = "04/15/2013";
Or if you want to use - then use the following line with the year in front:
$date = "2013-04-15";
Actually I wanted same alike thing,
To get one year backward date, for a given date! :-)
With the hint of above answer from #mohammad mohsenipur
I got to the following link, via his given link!
Luckily, there is a method same as date_add method, named date_sub method! :-)
I do the following to get done what I wanted!
$date = date_create('2000-01-01');
date_sub($date, date_interval_create_from_date_string('1 years'));
echo date_format($date, 'Y-m-d');
Hopes this answer will help somebody too! :-)
Good luck guys!

PHP date showing '1970-01-01 ' after conversion

I have a form in which date format is dd/mm/yyyy . For searching database , I hanverted the date format to yyyy-mm-dd . But when I echo it, it showing 1970-01-01 . The PHP code is below:
$date1 = $_REQUEST['date'];
echo date('Y-m-d', strtotime($date1));
Why is it happening? How can I format it to yyyy-mm-dd?
Replace / with -:
$date1 = strtr($_REQUEST['date'], '/', '-');
echo date('Y-m-d', strtotime($date1));
January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.
In other words, your strtotime($date1) returns 0, meaning that $date1 is passed in an unsupported format for the strtotime function.
$date1 = $_REQUEST['date'];
if($date1) {
$date1 = date( 'Y-m-d', strtotime($date1));
} else {
$date1 = '';
}
This will display properly when there is a valid date() in $date and display nothing if not.
Solved the issue for me.
$inputDate = '07/05/-0001';
$dateStrVal = strtotime($inputDate);
if(empty($dateStrVal))
{
echo 'Given date is wrong';
}
else{
echo 'Date is correct';
}
O/P : Given date is wrong
Another workaround:
Convert datepicker dd/mm/yyyy to yyyy-mm-dd
$startDate = trim($_POST['startDate']);
$startDateArray = explode('/',$startDate);
$mysqlStartDate = $startDateArray[2]."-".$startDateArray[1]."-".$startDateArray[0];
$startDate = $mysqlStartDate;
The issue is when your data is set to 000-00-00 or empty you must double-check and give the correct information and this issue will go away. I hope this helps.
Use below code for php 5.3+:
$date = new DateTime('1900-02-15');
echo $date->format('Y-m-d');
Use below code for php 5.2:
$date = new DateTime('1900-02-15');
echo $date->format('Y-m-d');
finally i have found a one line code to solve this problem
date('d/m/Y', strtotime(str_replace('.', '-', $row['DMT_DATE_DOCUMENT'])));

Categories