0

I want to convert the php date to timestamp. But i'm getting same output even I change the date in my URL.

Example: Imagine that I have URL that will get the value.

url: localhost/index.php?text_checkin=22/06/2018

$checkin = strtotime($_GET['text_checkin']);
$textin = date('y/m/d', $checkin);
echo $textin;

OUTPUT: 70/01/01

Please help me out of this problem.

4 Answers 4

3

Use date_create_from_format instead

working demo : https://eval.in/1018426

$checkin = $_GET['text_checkin'];

$dateObj = date_create_from_format('d/m/Y',$checkin);
echo date_format($dateObj,'Y/m/d');

Output

2018/06/22

For more : http://php.net/manual/en/datetime.createfromformat.php

Sign up to request clarification or add additional context in comments.

Comments

1

this format is not recognized as a valid date by strtotime.. try using the DateTime object, like this:

$dateTimeObject = DateTime::createFromFormat('d/m/Y', '22/06/2018');
echo $dateTimeObject->getTimestamp();

Comments

0
$mCheckIn = $_GET['text_checkin'];//getting the check in date
$mDateObj = date_create_from_format('d/m/Y',$mCheckIn);//creating a date object.  
echo date_format($mDateObj,'Y/m/d');//echo or store the results in a variable

Output:

2018/06/22

Try that

Comments

0

If you know the format will always be the same then you can replace the / with - which will make it a valid format for strtotime.

$checkin = strtotime(str_replace("/","-","22/06/2018"));
$textin = date('y/m/d', $checkin);
echo $textin;

This is probably lighter than calling date_create_from_format.
The flip side is that date_create_from_format will only work on this specific format.
If the format is Y-m-d this code will work too but date_create_from_format will not.

https://3v4l.org/Hkrop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.