PHP script can not format the date [duplicate] - php

This question already has answers here:
convert php date to mysql format
(9 answers)
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a datepicker that shows the date in the following format: date('d/m/Y') the date is represented as: 29/06/2017.
The date column in my database accepts the format: Y-m-d. Witch is represented as: 2017-06-29.
I am trying to convert the date with the following script:
$single_cal4 = date("Y-m-d",strtotime($single_cal4));
When I execute the PHP script that sends the date to the database the date will not insert. I get the date 1970-01-01 in my database.
Does someone know why this is happening and what I am doing wrong?

Maybe reason is $single_cal4. You make sure that $single_cal4 is valid date string. In other way you can try this:
$myDateTime = DateTime::createFromFormat('d/m/Y', $single_cal4);
$newDateString = $myDateTime->format('Y-m-d');

Related

How to get a format of the given date or time in php? [duplicate]

This question already has answers here:
PHP function to get the date format?
(6 answers)
Closed 11 months ago.
How to get a format of the given date or time in php?
If I give date like this,
2022-03-08 06:45:06
It should return "Y-m-d H:i:s"
Is there any possible way to get the format from date?
i dont think there is such a function.
if you need to handle dates in different formats, reformat them in timestamp and then in the needed format
you can try something like this
$timestamp = strtotime("2022-03-08 06:45:06");
$newFormat = date('Y-m-d', $timestamp)

Change Date format from 2018-09-27T18:30:00.000Z to 2018-09-27 13:17:14 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
When I select a date using angular md-datepicker I receive date in 2018-09-27T18:30:00.000Z this format but in my database i have dates in 2018-09-27 13:17:14 this format, how can I convert dates in PHP to search in the database please help me.
First create DateTime object then change to format whatever you want.
$dateTime = new DateTime('2018-09-27T18:30:00.000Z');
$databaseFormat = $dateTime->format('Y-m-d H:i:s');

Change type of date in html table - php mysql [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need some help with converting the date to dd/mm/YYYY.
I have this code to display my date in a html table from a mysql database
echo "<td>".$zeile['datumFahrt']."</td>";
Can I convert it in this line so the format will be dd/mm/YYYY instead of YYYY/mm/dd?
Sure you can, first you need to convert it to timestamp and then format it:
date("d/m/Y", strtotime($zeile['datumFahrt']))

SQL,PHP: Converting Date Format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I'm having a bit of a problem where both the SQL function and the php function does not convert dates properly when they're in the format of "DD-MM-YYYY". I am using a mysql database and I want to convert the date to "YYYY-MM-DD" before it is entered into the database.
The SQL function:
INSERT INTO lease
VALUES(3, 4, STR_TO_DATE('22-02-2015', '%Y-%m-%d'), STR_TO_DATE('27-02-2015', '%Y-%m-%d'))
Returns as "2022-02-20" and "2027-02-20"
Also, the php function puts the month and day in the wrong place so I have to do "Y-d-m" instead, like so:
$startdate = date("Y-d-m", strtotime('27-02-2015'));
$enddate = date("Y-d-m", strtotime('23-02-2015'));
Although that stores it correctly. Any date where the day is > 12 will reset the date to"1970-01-01" because it thinks the day is the month.
Can anyone help with this?
The format argument for STR_TO_DATE() is the format used for the string argument
So use
STR_TO_DATE('22-02-2015', '%d-%m-%Y'),

php formatting dates for mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
convert php date to mysql format
I have a date field which lets users type in their birthday in m/d/Y format (for example 06/01/1982) but just in case I also made my validation accept month and day values without any leading 0 (6/1/1982).
How can I convert these dates to Y-m-d for use in MySQL?
Other valid dates are 6/01/1982 and 06/1/1982.
You could use date() and strtotime() together like such:
$date = date('Y-m-d', strtotime('06/01/1982'));
That will give you
1982-06-01
I would validate all input using strtotime (which will handle all major date formats).
On the insert, cast it to the proper format using the inverse: strftime:
$userValidatedDate = strtotime($_GET['date']);
//...
$sqlDate = strftime('%Y-%m-%d',$userValidatedDate);

Categories