This question already has answers here:
PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa
(5 answers)
Closed 6 years ago.
Im currently struggeling with timestamps. I have Googled for a simple answer for this, but cant find any solution for my problem. I want to convert user input into UNIX time. The user inputs for example "26/03/1982", how do I convert this input into UNIX timestamp?
//User input
$birthday = "26/03/1982"; // d/m/y
$birthday = [CONVERTED TO UNIX]
Short and simple question. I will add more if you'd like. Thanks.
Look into the PHP function strtotime:
strtotime — Parse about any English textual datetime description into a Unix timestamp
I'm not sure how well it handles / in dates, but replace them with - and it'll work.
Example
echo strtotime(str_replace("/","-","26/03/1982"));
outputs 385977600 which is the Unix date for Fri, 26 Mar 1982
You may need to set your date_default_timezone for this to work
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
In a database, the date is stored like this: 14 May 2020 22:25
I was wondering, when reading this line from database, and change the output to this: 14-5-2020, is it possible to convert this easily? because sometimes i want to output 14 May 2020 22:25 and sometimes i want to output 14-5-2020. And if is possible, i only want to store 1 format in the database
<?php
$date = DateTime::createFromFormat('d M Y H:i', '14 May 2020 22:25');
echo $date->format('d-n-Y'); //14-5-2020
As mentioned in the comment by #Sammitch, you can use PHP DateTime Class to achieve this.
You can format the Date and Time, once you create it. Be sure to check the format guide. Given below as link.
Useful Links:
Formatting DateTime in PHP
PHP DateTime
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
Here are 3 examples of date and time strings I have
08-01-2019_03-00-34pm
22-01-2019_02-05-06pm
30-01-2019_11-17-07am
I would like to convert these to Unix Time Stamps
For example I have tried this on the first one..
echo (strtotime("08-01-2019"));
This half works, as it gives me a timestamp of 1546905600 which translates back to the correct date, but obviously makes the time 12:00:00am by default. So I'm stuck with how to deal with the time bit
I understand there is a str_replace() function so as long as I know what format I need to make the time bit, I guess I could use that?
Use DateTime::createFromFormat():
$date = DateTime::createFromFormat('d-m-Y_h-i-sa', '08-01-2019_03-00-34pm');
You can then use it to get the timestamp
$date->getTimestamp(); // 1546977634
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I am doing php parsing and getting date like Thu 1 Dec and I want to store it in database and my date column in database is of DATE type, so when I store this in database, it inserts 0000-00-00 because of improper format. How can I convert it into proper format?
To format the date properly, use something like this
$Date = Date('c', strtotime($Date))
The date has to be something strtotime can understand, but strtotime can understand a lot.
This question already has answers here:
PHP date format converting
(4 answers)
Closed 9 years ago.
I am fairly new to PHP programming and thus am facing a strange problem, through different forums i was able to learn how to convert dates of different formats into our desired formats, but today this was not possible and i was always given 01 Jan no matter what i gave in the variable, which was strange as previously it was working great. My code is following:
I am using a combination of date and strtotime PHP functions, as:
$coll_date_1[$i] = $row->coll_date_1; //this is 08-08 i.e 8 Aug
$coll_date_1[$i] = date('d M',strtotime($coll_date_1[$i])); //this return 01 Jan
The 1st line assigns value from database into the variable, and the 2nd line is the function to convert them into desired format. I know i am doing a silly mistake, but cant seem to pin point where that is...
That happens because 08-08 is not a valid date format.
You could try adding a "fake" year in front of it, like 2000-08-08, or the current year, like 2013-08-08, and than do
$coll_date_1[$i] = 'YEAR-'.$row->coll_date_1; // "YEAR" could be "2000" or the current year
date('d M', strtotime($coll_date_1[$i]));
Anyway, you should change your database to handle dates in the proper format, which should be YYYY-MM-DD, for example in MySQL you'd use a date type field.
EDIT:
If you cannot change the database structure, but have PHP 5.3+, you can use the DateTime class to parse the date as
DateTime::createFromFormat('m-d', $row->coll_date_1)->format('d M');
You can specify the format using DateTime::createFromFormat:
$date = DateTime::createFromFormat("m-d","08-08")//Switch to "d-m" as needed
$date->format("d M");
This makes it easier to change the before format and after format.
Edit: As has been noted. Consider storing your column as a proper date or datetime. This will allow you to make use of the DB's date functions should you need them.
//this is 08-08 i.e 8 Aug
While it should be 2013-08-08.
Change your database field to proper format. Problem solved.
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);