PHP - strtotime is ignoring the year - php

$date_string = "1 October, 2013";
$date_time = strtotime($date_string); //evaluates to "1412208780"
$date_string_again = date("m/d/y", $date_time); //evaluates to "10/01/14" .. should be "10/01/13"
Why is my code giving me the wrong year? How do I fix it?

Make use of DateTime::createFromFormat
<?php
$dt = '1 October, 2013';
$date = DateTime::createFromFormat('j F, Y', $dt);
echo $date->format('m/d/Y'); //"prints" 10/01/2013

Indeed, the comma is throwing it off. Remove it!
$date_string = "1 October, 2013";
$date_string = str_replace(',','',$date_string);
$date_time = strtotime($date_string);
$date_string_again = date("m/d/y", $date_time);

Your string is not a valid format that strtotime() recognizes, see the manual for accepted formats.
If the format is fixed like this, you could use DateTime::createFromFormat to convert your date to a DateTime object.

Related

Changed position from day and month converting strings to date

I am working on following issue in PHP.
I have these variables:
$fecha = "01-07-2017";
$hora = "11:30";
Now I want to create a date variable with format Y-m-d H:i.
For that I am using following code:
$newDate = date("Y-m-d H:i", strtotime($fecha.' '.$hora));
But I am getting the output:
echo $newDate = 2017-01-07 11:30
I need it to be: 2017-07-01 11:30
What am I doing wrong?
Use the DateTime API to parse your string(s) initially
$dt = DateTime::createFromFormat('d-m-Y H:i', $fecha.' '.$hora);
$newDt = $dt->format('Y-m-d H:i');
Demo ~ https://eval.in/827662
Change your code to:
$newDate = date("Y-d-m H:i", strtotime($fecha.' '.$hora));
Notice that I just switched around 'h' & 'd' in the date function.

amazing result of date format in php

I am trying to change the date 13 December, 2016 using date('Y-m-d',strtotime('13 December, 2016')) and it gives me the result of 2017-12-13, what i am missing here
Actually that comma within the date strings create an issue while using the strtotime function you can replace it with str_replace or instead you can simply use DateTime::createFromFormat method so no need of using extra function like as
$date = DateTime::createFromFormat('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
or simply date_create_from_format
$date = date_create_from_format('d F, Y','13 December, 2016');
echo $date->format('Y-m-d');
date('Y-m-d', strtotime('13 December 2016'));
You need to remove your comma.
This will give the expected result.
Try this
$date = '13 December 2016';
echo date('d-m-Y', strtotime($date));
The m formats the month to its numerical representation there.
Mistake: You don't have to put coma there.
use
$time = strtotime('10/21/2016');
$newformat = date('Y-m-d',$time);
echo $newformat;

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 time format conversion

I have the following timeformat:15/08/2011 12:32:23 day/month/year hour:minute:sec and I want to convert to the following format: Y-m-d H:i:s
I tried with date('Y-m-d H:i:s', strtotime($time)) but it not works. It swaps the month and the day when it's converting from string to datenum.
strtotime understands both American (mm/dd/YYYY) and European (dd-mm-YYYY or dd.mm.YYYY) formats. You are using slashes to separate day, month and year, and that's why your date is interpreted as American. To solve that, replace the slashes with dashes.
In which case, you could very simply swap the month and date from your string:
$time_string = "15/08/2011 12:32:23";
$strtotime = explode("/",$time_string);
$strtotime = implode("/",array($strtotime[1], $strtotime[0], $strtotime[2]));
echo date('Y-m-d H:i:s', strtotime($strtotime));
Working Example: http://codepad.viper-7.com/234toO
You are going to have to reparse the time. strtotime is thinking you are trying to input a string in the format of 'm/d/Y H:i:s', but you are supplying 'd/m/Y H:i:s'.
list($date, $times) = explode(' ', $time);
list($day, $month, $year) = explode('/', $date);
$newTime = date('Y-m-d H:i:s', strtotime("$month/$day/$year $times");
Replace slashes with hyphen in the date then try it will work.
$a = '07/08/2019'; // 07 is day 08 is month.
echo date('Y-m-d', strtotime($a)); //output: 2019-07-08 where 07 became month.
$a = str_replace("/","-","07/08/2019"); // 07-08-2019
echo date('Y-m-d', strtotime($a)); //2019-08-07

Categories