php convert date to mktime function

0 votes

I posted a date input $_POST['date'] with format 2013/11/22 and time $_POST['time'] with format 10:10 AM. Now I need to put both inputs in the mktime function like this:

mktime(10, 10, 0, 11, 22, 2013);

How can I create this

Jun 27, 2022 in PHP by narikkadan
• 63,600 points
555 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

To generate a DateTime object from any format, use the DateTime::createFromFormat method. The timestamp may then be obtained using that object (or the date can be formatted in another way):

// $_POST['date'] = '2013/11/22';
// $_POST['time'] = '10:10 AM';

$datetime = $_POST['date'] . ' ' . $_POST['time'];
$datetime = DateTime::createFromFormat('Y/m/d h:i A', $datetime);

if ($datetime) {
    $timestamp = $datetime->getTimestamp();
} else {
    echo 'Invalid date or time.';
}

All elements of the input should have leading zeros according to the format in my solution (Y/m/d h: I A) (e.g., 2013-01-01 01:01 AM). You will need to modify the input format if the input doesn't utilize leading zeros. The manual lists all the supported format characters.

answered Jul 21, 2022 by Kithuzzz
• 38,000 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?

Hii @kartik, Working with different servers with various ...READ MORE

answered Apr 20, 2020 in PHP by Niroj
• 82,840 points
7,416 views
0 votes
1 answer

How to Convert timestamp to readable date/time PHP?

Hello @kartik, Use PHP's date() function. Example: echo date('m/d/Y', 1299446702); Hope it helps!! Thank ...READ MORE

answered Sep 14, 2020 in PHP by Niroj
• 82,840 points
2,320 views
0 votes
0 answers

convert strtotime to date time format in php

I have to use PHP to convert ...READ MORE

Jul 21, 2022 in PHP by narikkadan
• 63,600 points
873 views
0 votes
1 answer

How to convert from MySQL datetime to another format with PHP?

Hello, To convert a date retrieved from MySQL ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,840 points
3,815 views
0 votes
1 answer

How to Execute PHP function with onclick?

Hello @kartik, In javascript, make an ajax function, function ...READ MORE

answered Jun 16, 2020 in PHP by Niroj
• 82,840 points
24,733 views
0 votes
1 answer

How to call a php function from ajax?

Hello @kartik, You can't call a PHP function ...READ MORE

answered Jun 16, 2020 in PHP by Niroj
• 82,840 points
12,946 views
0 votes
1 answer

How do I get the current date and time in PHP?

The time would go by your server ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
834 views
0 votes
0 answers

What php timezone I use for San Francisco, California, United Stated

What PHP time zone can I use ...READ MORE

May 27, 2022 in PHP by Kichu
• 19,040 points
813 views
0 votes
0 answers

Convert one date format into another in PHP

How can I convert one date format ...READ MORE

Jun 20, 2022 in PHP by Kithuzzz
• 38,000 points
365 views
0 votes
0 answers

How to calculate the difference between two dates using PHP?

I have two dates on the form: Start ...READ MORE

Jun 24, 2022 in PHP by narikkadan
• 63,600 points
882 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP