Date Time format without time - php

I have a variable holding time in the format Apr 25, 2017 12:00:00 AM I wish to convert it to 2017-04-25 .My result should be without time

You can convert your string formatted date, to timestamp with strtotime() function. Then you can parameter date() function with converted timestamp, and format it with first parameter.
More about formatting:
http://php.net/manual/en/function.date.php
print date('Y-m-d',strtotime('Apr 25, 2017 12:00:00'));

You need to check the date method of PHP
$date = 'May 25, 2017 12:00:00 AM';
$date = date('Y-m-d', strtotime($date));
echo $date;

Try DateTime class:
$datestr = 'Apr 25, 2017 12:00:00 AM';
$date = DateTime::createFromFormat('M d, Y h:i:s A', $datestr);
echo $date->format('Y-m-d');
output
2017-04-25

Use the date() function:
$old_date="Apr 25, 2017 12:00:00 AM";
$new_date=date('Y-m-d', strtotime($old_date));
echo $new_date;

Related

how to convert DateTime format [2016-10-05 11:58:04] using DateTime::createFromFormat

i am trying to display a stored DateTime with this format [2016-10-05 11:58:04]. What i want to do is, display the stored date into this readable format [Wed, Oct 10, 2016].
Use date() function. First param is format and 2nd param is timestamp:
$time = "2016-10-05 11:58:04";
echo date("D, M d, Y", strtotime($time)); //output: Wed, Oct 05, 2016
If you want to use datetime object then:
$time = "2016-10-05 11:58:04";
$date = new DateTime($time);
echo $date->format('D, M d, Y'); //output: Wed, Oct 05, 2016
you can use the method format to choose what to display :
<?php
$d = DateTime::createFromFormat("Y-m-d H:i:s", "2016-10-05 11:58:04");
var_dump($d->format("c"));
look the help here : http://php.net/datetime.format

Converting date (with milliseconds) into timestamp

I have date format like '25 May 2016 10:45:53:567'.
I want to convert into the time stamp.
strtotime function returns empty.
$date = '25 May 2016 10:45:53:567';
echo strtotime($date);
// returns empty
When I removed the milliseconds, it's working.
$date = '25 May 2016 10:45:53';
echo strtotime($date);
// returns 1464153353
Please sort out my issue. Thanks in advance.
Use DateTime:
$date = DateTime::createFromFormat('d M Y H:i:s:u', '25 May 2016 10:45:53:000');
echo $date->getTimestamp();
// 1464165953
// With microseconds
echo $date->getTimestamp().'.'.$date->format('u');
// 1464165953.000000
Split string:
$date = '25 May 2016 10:45:53:001';
preg_match('/^(.+):(\d+)$/i', $date, $matches);
echo 'timestamp: ' . strtotime($matches[1]) . PHP_EOL;
echo 'milliseconds: ' . $matches[2] . PHP_EOL;
// timestamp: 1464162353
// milliseconds: 001
Use Datetime instead of date and strtotime.
//using date and strtotime
$date = '25 May 2016 10:45:53:000';
echo "Using date and strtotime: ".date("Y-m-d H:i:s.u", strtotime($date));
echo "\n";\
//using DateTime
$date = new DateTime();
$date->createFromFormat('d M Y H:i:s.u', '25 May 2016 10:45:53:000');
echo "Using DateTime: ".$date->format("Y-m-d H:i:s.u");
// since you want timestamp
echo $date->getTimestamp();
// Output
// Using date and strtotime: 1969-12-31 16:00:00.000000
// Using DateTime: 2016-05-28 03:25:22.000000
Example

php variable date format

I have a variable
$date
Which contains a date in the following format:
'Monday, 24 August 2015'
Or as a strtotime:
'1440716400'
Id like to change it to the following format:
'24/08/2015'
How can I do this please?
Simply date()
$timestamp = strtotime('Monday, 24 August 2015');
echo date("d/m/Y", $timestamp); // Here you put the format you want
Try
date("d/m/Y", strtotime('Monday, 24 August 2015'));

Transform date to valid format for PostgreSQL with PHP

I have a string in this format:
Thu, 26 Feb 2015 11:39:59
And I wonder if its possible to transform it to a valid timestamp format in order to insert it in my PostgreSQL database.
So ideally in the end I would have something like:
2015-03-26 11:39:59
Is there a function in PHP to do something like this?
Use DateTime() to format date string.
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = new DateTime($date);
echo $date->format('Y-m-d H:i:s');
You can also use it as DateTime::createFromFormat
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = DateTime::createFromFormat('D, d M Y H:i:s', $date);
echo $date->format('Y-m-d H:i:s'); //2015-03-26 11:39:59

convert from a string into a date in php

I have looked around but keep getting an error.
I have the following string:
$date = "25 Jan 2013 07:30 PM";
but I need to add it into sql as separate date and time fields. Any suggestions how I can convert it to a date rather than a string? When I try to using something like
strtotime($date);
I end up with:
1359142200
Option 1:
$date = date("Y-m-d", strtotime("25 Jan 2013 07:30 PM"));
$time = date("H:i:s", strtotime("25 Jan 2013 07:30 PM"));
Option 2
$datetime = DateTime::createFromFormat("j M Y H:i A", "25 Jan 2013 07:30 PM");
$date = $datetime->format("Y-m-d");
$time = $datetime->format("H:i:s");
For best results, convert $date to a DateTime object with DateTime::createFromFormat and then use DateTime::format to get the values for your separate fields.
Example:
$dt = DateTime::createFromFormat("d M Y H:i A", "25 Jan 2013 07:30 PM");
$date = $dt->format("Y-m-d");
$time = $dt->format("H:i:s");
So you just want them separated? Something like:
$year_month_day = date('Y/m/d', strtotime($date));
$timestamp = date('H:i:s', strtotime($date));
More info: http://www.php.net/manual/en/function.date.php
If you want it as a timestamp then strtotime() is the PHP function to use:-
http://php.net/manual/en/function.strtotime.php
If you want it in a MySQL date format string then strtotime() in combination with date() would be the answer like this:-
$db_date = date("Y-m-d H:i:s", strtotime($date) );

Categories