Php time format conversion - php

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

Related

Sum date using strtotime always returns 01/01/1970

I am trying to add 1 day to a date using strtotime but I can´t get it to work. It always returns 02/01/1970
$date = date ("d/m/Y H:i:s", filemtime($directory));
$newdate = date("d/m/Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970
Because strtotime() differentiates between USA date format and Sensible date format by looking at the date seperator all you need to do is us the - seperator if you want to use the sensible date format, like this, in your intermediate date manipulations
$date = date ("d-m-Y H:i:s", filemtime($directory));
$newdate = date("d-m-Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970
FROM the manual
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Better work with DateTime():
$date = new DateTime(strtotime(filemtime($directory)));
echo $newdate = $date->format('d/m/Y');
$date->modify('+1 day');
echo $tomorrow = $date->format('d/m/Y');
Output:
20/01/2020
21/01/2020
If your filemtime($directory) returns a string formatted as your date() mask, I mean d/m/Y H:i:s, then you can do next steps:
for example, according to this mask, it looks like:
$s = "02/06/2019 22:23:22";
now you can do strtotime()
$date = date ("d/m/Y H:i:s", strtotime($s));
then transform it to the DateTime object
$st_date = new DateTime($date);
now you can simply modify it as you want
$st_date->modify('+1 days');
to see the result string value use:
$tomorrow = $st_date->format('d/m/Y');
echo 'tomorrow -> '.$tomorrow;
Output:
date->02/06/2019 22:23:22
tomorrow -> 03/06/2019
Demo

how to do general conversion of date from different formats (some times with extra string) to Y-m-d format

In my CodeIgniter application, I am getting date in different formats, such as: April 1st 2017, May 29, 2015, Jun-15-2015, 10-September-2015 and sometimes even with extra string such as Start: April 1, 2017. However, I want to convert the input date from any format to Y-m-d in order to save it in MySQL database. For example, if input date is April 1st 2017 I should get 2017-04-01. I have used below posted code for that but it is not working for all of the above mentioned cases. So please tell how can I write general conversion logic that can convert date from any format even if date has extra string with it (as mentioned above) to Y-m-d format.
Code:
$date = DateTime::createFromFormat('F jS Y', $old_date);
$new_date = $date->format('Y-m-d');
try this
$old_date = 'Jun-15-2015';
echo $newDate = date("Y-m-d", strtotime($old_date));
You can replace the extra string like
$date = str_replace('Start: ',' ',$date);
And after you can use date function of php
echo $date = date('Y-m-d',strtotime($date));
This might help even for inserting this date format into database tables
function convertUTCCombinedToLocal($utcDateTime) {
$utcDateTime = explode(" ", $utcDateTime);
$date = explode("-", $utcDateTime[0]);
$time = explode(":", $utcDateTime[1]);
$localDate = new DateTime();
$localDate->setTimezone(new DateTimeZone('UTC'));
$localDate->setDate($date[0], $date[1], $date[2]);
if (count($time) == 3) {
$localDate->setTime($time[0], $time[1], $time[2]);
} else {
$localDate->setTime($time[0], $time[1], '00');
}
$localDate->setTimezone(new DateTimeZone('Asia/Kolkata'));
return $localDate->format('d/m/Y H:i:s');
}

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.

PHP strtotime returns false

I have a jquery function which passes the date '27/05/2016 11:25 PM' to a PHP file. The PHP file will update this to the database.
I'm using strtotime to convert this string to date format '05/27/2016 11:25 PM' but strtotime returns false.
My PHP date conversion:
$EndDate = strtotime($Date);
$NewEndDateValue = date('m/d/Y h:i A', $EndDate);
var_dump($NewEndDateValue); //this returns false
strtotime() by default treats dates with / seperators as the wierd USA format for dates, where they start in the middle of a date and work outwards from there (go figure). Its fine to speak it that way but totally illogical to expect a logic machine (computer) to work that way.
Anyway, all you need to do is convert the / to a - and date() will assume a logical date format and therefore work.
<?php
$Date = '27/05/2016 11:25 PM';
$dat = str_replace('/', '-', $Date);
$EndDate = strtotime($dat);
$NewEndDateValue = date('m/d/Y h:i A', $EndDate);
var_dump($NewEndDateValue); // "05/27/2016 11:25 PM"

Date format PHP

I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables
// unix
$date = 1436713200
// string
$time = '15:00';
to get a date format 2015-07-12 15:00 but failing, using this
$newdate = date('Y-m-d H:i:s', $date.' '.$time);
I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.
I would suggest you to use DateTime instance to avoid timezone issues:
$d = date_create('#1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00
You do not have to provie the $time variable. Unix time is a full date with time.
Use:
$newdate = date('Y-m-d H:i:s', $date);
Use this
$date = date('Y-m-d','1436713200');
// string
$time = '15:00';
echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));

Categories