php not converting dates - php

This might be very simple, but I still need help with this.
I have this portion of php code:
$END = NULL;
if (isset ($_POST['end'])){
$END = date_format($_POST['end'], "Y-m-d H:i");
}
Where $_POST['end'] is a date and time that I get in format dd-mm-YYYY HH:mm. The problem is, as you can guess, that it doesn't transform my input to the Y-m-d H:i format, it just doesn't do anything. But I've followed what I've seen in another code that does indeed work. What am I doing wrong here?
Ignore the fact that I don't check if the input is well written, I assume that it will be.

This is because date_format accepts an object and not a string. You should use the function date and pass to it's second argument a timestamp.

Use date_create() function to convert string to date and then pass it to date_format() function.
if (isset ($_POST['end'])){
$date = create_date( $_POST['end'] );
$END = date_format($date, "Y-m-d H:i");
}

Related

DateTime function unix timestamp converting wrong

I am using DateTime function of php. I get a date from a calendar in format d-m-Y and pass it via ajax to my function. I am getting the date right till this step.
When I try to store the date in unix format using:
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y', $data['date']);
$final_date=$ai_ff_date->format('U');
The date stored is wrong. Suppose the date I passed via ajax is 26-12-2016 then in database 27-12-2016 is stored. Why its counting one more day then the input.
use this code :
$date = date('Y-m-d H:i:s', strtotime('-1 day', $stop_date));
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y',$date);
$final_date=$ai_ff_date->format('U');
and please check the variable (code not tested)
You might want to convert the Date-Format to "Y-m-d" First and then call-in the DateTime() Constructor. However, since what you are trying to do is just get the TimeStamp you might also do that directly without using DateTime. The Snippet below shows what is meant here:
<?php
$data = ['date'=>"13-12-2016"]; //<== JUST AN EXAMPLE FOR TESTING!!!
// SIMPLY CONVERT THE DATE TO Y-m-d FIRST.
$dateYMD = date("Y-m-d", strtotime($data['date']));
// THEN USE DateTime CONSTRUCTOR TO CREATE A NEW DateTime INSTANCE
// AND THEN RUN THE FORMAT YOU WISH::
$final_date = (new DateTime($dateYMD))->format('U');
var_dump($final_date); //<== YIELDS: string '1481583600' (length=10)
var_dump(date("Y-m-d", $final_date)); //<== YIELDS: string '2016-12-13' (length=10)

strtotime() doesn't recognize german shortform of date (dd.mm.YY)

I have much data with several timestamps and I just recognized that some are in "dd.mm.YYYY" which works very well with date("Y-m-d", strtotime($input)); but some are in "dd.mm.YY" and this does not work anymore - it always returns the current date.
My problem is that my data is too huge to fix this problem manually by editting. Is there any way to get the YYYY-mm-dd out of mm.dd.YY ?
Here you go...
$date = "20.02.71"; // sample date... (common German format)
$date = DateTime::createFromFormat('d.m.y', $date);
echo $date->format('Y-m-d');
will result in:
1971-02-20
Create a DateTime object, then format it to anything you want...
Well you can replace the . by -, you could do something like the following:
$date = str_replace(".", "-", "mm.dd.YY")
This would return
mm-dd-YY
You could use date_parse_from_format which would convert any formate into the formate you specify.
date_parse_from_format("y-m-d", $date);
It returns an array with very useful information like month, year etc.

DateTime defaulting to 1970-01-01

I am writing a PHP form for my website. The HTML side asks the user for a date which they enter in MM/DD/YYYY format. When that string is sent to PHP the following code changes it to the form that MySQL will recognize
$date = $_POST['date'];
$sqldate = date('Y-m-d', strtotime($date));
However when that date is entered into my MySQL database it is entered as 1970-01-01 and I can't figure out why.
NOTE: If I echo $sqldate I get the error Use of undefined constant sqldate - assumed 'sqldate' in C:/MYDIRECTORY
How about this?
//build a date
$date = date_parse_from_format("m/d/Y", $_POST["date"]);
//output the bits
$sqldate = "$date[year]-$date[month]-$date[day]";
A Unix timestamp is the number of seconds since 1970-01-01 so your call to strtotime() is returning 0.
How is DateTime working for you?
<?php
$dateStr = '08/18/2014';//$_POST['date'];
$dateTime = new DateTime($dateStr);
echo $dateTime->format('Y-m-d');
You're writing that the date format of the form is MM/DD/YYYY. Do you validate the input values to be sure that the format is always given?
THis worked for me
$input_date= trim($_POST["input_date"]);
$strtotime= strtotime($input_date);
$date_format= date('Y-m-d',$strtotime);

strtotime () not converting to timestamp

I have a loop and in it the date is coming in different formats like for some values it will be like '10-13-2013 04:31' and for some it is like '2013-10-14T22:14:40-0700'. I tried to store this in DB as the value of a datetime/timestamp column but it is failing for the first format that is 10-13-2013 04:31. So I tried to convert it into UNIX timestamp using strtotime(). It is working for some values and is storing zero for values like '10-13-2013 04:31'. I think this is because it is considering the second value as month and so failing. My code is as follows :
foreach($reports as $report){
echo strtotime($report->transactionDate);
}
strtotime() is unable to parse mm-dd-yyyy format. Instead you should use DateTime::createFromFormat(), like this:
$date = '10-13-2013 04:31';
$obj = DateTime::createFromFormat('m-d-Y H:i', $date);
$date = $obj->format('Y-m-d H:i:s');

Generate a data-time format from 2 separated strings

I need to compare date/time that I get from MySQL with a format like: 2013-05-17 15:07:29
From another database, I have data and time separated and in the notation: 130998 081836
I have concatenated the two strings to get only one and I'm trying to convert it to my desired format using:
$dateTimeNmea = $array[9]." ".$array[1]; // 130998 081836
$dateTime = date("Y-m-d H:i:s", $dateTimeNmea); // 1970-01-02 13:23:18
So "it works" on the format but the values are wrong. It could be 1998-09-13 08:18:36
Where is my fault?
It has format siH dmy. Try date_parse_from_format('siH dmy', $string) to get it in array.
130998 081836 is not a format for a date that the date function can understand.
For starters the date() function expects the second param to be a timestamp (read docs for it here)
Then you would need to parse the string into a useable date format via date_parse_from_format and finally into a timestamp
Something like
$string = '130998 081836';
$date = date_parse_from_format('dmY His', $string);
$dateString = date('Y-m-d H:i:s', strtotime(vsprintf('%s-%s-%s %s:%s:%s', $date)));
var_dump($dateString); // var dump just for output/test
The strtotime(vsprintf('%s-%s-%s %s:%s:%s', $date) formats your parsed date into a timestamp which can then be used in date methods second param to get exactly the format you need.

Categories