I need to convert DD/MM/YYYY h:m eg 21/12/2015 17:14 this date to timestamp format using php and store it in mysql.
I have tried strtotime() and some other functions but it's not working. Can any one help me on this. I'm using datepicker to generate the value for date time
Use following code to convert "DD/MM/YYYY h:m" into timestamp
$dateString = "21/12/2015 17:14";
$timestamp = strtotime(str_replace("/", "-", $dateString));
you can use DateTime class:
$dateStr = '21/12/2015 17:14';
echo $timestamp = \DateTime::createFromFormat('d/m/Y H:i', $dateStr)->getTimestamp();
Related
I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));
I have made many searches for a simple function to convert custom DateTime to Timestamp, But I didn't get the exact solution:
The DateTime str is:
$dt = "14-05-2015 18:21";
(Day-Month-Year Hour:Minute) (not the standard DateTime format)
Is there a simple function to convert this string to timestamp?
Thanks.
can't be simpler than:
$dt = "14-05-2015 18:21";
echo strtotime ($dt); //1431627660
If format will always be the same, you can do it like this.
$date = DateTime::createFromFormat('d-m-Y H:i', $dt);
$timestamp = $date->format('U');
I'm trying to format a date in the form mm-dd-yyyy to the form yyyy-mm-dd, however, when I try formatting it, it comes out as 1969-12-31.
Here's my code:
$custom_date = "10-13-2013";
$formatted_date = date("Y-m-d", strtotime($custom_date));
What's wrong?
$custom_date = "10-13-2013";
$formatted_date = DateTime::createFromFormat("m-d-Y", $custom_date)->format("Y-m-d");
mm-dd-yyyy is not a format that is recognised by strtotime. That's because it wouldn't reliably be able to handle dates like 03-04-2013, it is the fourth of March or the third of April?
You need to parse it manually, or use the DateTime class.
list($m,$d,$y) = explode("-",$_GET['date']);
$timestamp = mktime(0,0,0,$m,$d,$y);
$formatted_date = date("Y-m-d",$timestamp);
This is my date format:
d.m.y
but I need it like dd.mm.yy
How can with php check format and if is d.m.y then convert it to dd.mm.yy and if is already dd.mm.yy leave it.
If is y-m-d conver it to yy-mm-dd
How can i do with regex?
Why regex. Because my datum is CONSTANT. And for each installation is different. But for some cases must be not d.m.y but dd.mm.yy for example.
but can be some other format also.
$show_date = DateTime::createFromFormat('d.m.Y', $dateInput)->format('Y-m-d');
DateTime
You can use
$date = date('d.m.y', strtotime($your_date))
or
$date = new DateTime($your_date);
echo $date->format('d.m.y');
Try this :
$date = new DateTime('2000-01-01'); /// you need to convert you input date to this format
echo $date->format('d-m-y');
iam having the date format stored in database as "20100723"(YYYYMMDD) and how do i convert it into "23-JUL-2010"
$original = "20100723";
$converted = date("d-M-Y", strtotime($original));
See manual: date(), strtotime()
if php >= 5.3.0
<?php
$date = DateTime::createFromFormat('YYYYMMDD', $dbStoredTime);
echo $date->format('d-M-Y');
strtoupper(date_create("20100723")->format("d-M-Y"))
See the DateTime class, in particular the DateTime::format method. date_create is an alias to the constructor of DateTime.
Use PHP's strtotime() function, it recognizes the date format in the string and converts it into a Unix timestamp.
$time = strtotime("20100723"); // 1279836000
Then just use date() to convert it back into another string format
echo date("d-M-Y", $time); // 23-Jul-2010
Note that you would have to use strtoupper() to make "Jul" upper-case