How to convert date part of datetime to timestamp in PHP? - php

How can I just convert date part of datetime to timestamp?
For example in this datetime: 2018-02-26 20:30:00
I want timestamp of 2018-02-26 00:00:00.

If you prefer using the DateTime API you could also do:
$dateAndTime = '2018-02-26 20:30:30';
DateTime::createFromFormat('Y-m-d H:i:s', $dateAndTime)->setTime(0, 0)->getTimestamp();

If you use substr you only need to use strtotime once and no date.
Echo strtotime(substr("2018-02-26 20:30:00",0,10));
Or you can use explode:
Echo strtotime(explode(" ", "2018-02-26 20:30:00")[0]);
I explode on space and use the first item [0]

<?php
echo strtotime(date("Y-m-d",strtotime("2018-02-26 20:30:00"))." 00:00:00");

Here you go. Just use strtotime twice. Once to set your date and then the second to generate the timestamp.
strtotime() can be used to convert just about any human readable date string to a timestamp.
http://php.net/manual/en/function.strtotime.php
$str = '2018-02-26 20:30:30';
echo date('Y-m-d', strtotime($str)) . '<br>';
echo strtotime(date('Y-m-d', strtotime($str)));

An easy and best solution would be this one:
$datetime = '2018-02-26 20:30:00';
$new_date = strtotime( Date( 'Y-m-d', strtotime( $datetime ) ) );

Try this:
$date = "2018-02-26 20:30:00";
echo strtotime(date('Y-m-d 00:00:00', $date));
This outputs UNIX timestamp. You can remove strtotime to get gregorian calendar

Related

Date Format Conversion in PHP 5.1.2

1.2 and need to convert a date from dd/mm/yyyy to yyyy-mm-dd
For example if the date is in format 07/08/2014, it should appear as 2014-08-07
How can this be done? I know strtotime returns unix timestamp but it doesn't seem to work with dates with Slashes (/) in it. SInce I'm using 5.1, a lot of DateTime functions are not supported in it.
Please help.
Use DateTime class, strtotime function would create issue when date less then 1901 with PHP 5.3.0
Try this way
$date = DateTime::createFromFormat('d/m/Y', "07/08/2014");
$new_date_format = $date->format('Y-m-d');
Need to pass a correct format with -(date string separation with dash) in date() try
$d = str_replace('/', '-','07/08/2014');
echo date('Y-m-d', strtotime($d)); //2014-08-07
with DateTime
$objDateTime = new DateTime($d);
echo $objDateTime->format('Y-m-d'); //2014-08-07
You can do it by date('Y-m-d',strtotime($date))
Where $date is in any format that you want to convert to YYYY-MM-DD format.
By using date() function yuo can try this
echo date('Y-d-m',strtotime('07/08/2014'));
Check the documentation for more
Method : 1 demo
$date1 = "07/08/2014";
$arr = explode("/", $date1);
$date2 = $arr[2]."-".$arr[1]."-".$arr[0];
echo $date2;
Method : 2 demo
$date1 = "07/08/2014";
list($day, $month, $year) = explode("/", $date1);
$date2 = $year."-".$month."-".$day;
echo $date2;
Method 3 : with strtotime Demo
$date1 = "07/08/2014";
$date1 = str_replace("/", "-", $date1);
$date2 = date('Y-m-d', strtotime($date1));
echo $date2;

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

converting between date formats

how would you convert a date stored as
2011-01-18 11:51:41
into
18-01-2011 11:51:41
using PHP?
many thanks in advance!
date('d-m-Y H:i:s', strtotime('2011-01-18 11:51:41'));
More reliable than using strtotime(), assuming you're on PHP 5.3+
$oldtime = date_parse_from_format('Y-m-d h:i:s', '2011-01-18 11:51:41');
$newtime = date('d-m-Y h:i:s', $time);
However, the date format you're converting FROM suggests it's coming from a MySQL datetime field, in which case you could also do:
SELECT DATE_FORMAT(yourfield, '%d-%m-%Y %H:%i:%s')
and save yourself a full roundtrip in PHP.
Convert the old date to UNIX time with strtotime(), then output it in the new format with date()
$olddate = "2011-01-18 11:51:41";
$newdate = date('d-m-Y H:i:s', strtotime($olddate));
echo $newdate;
// 18-01-2011 11:51:41
$your_date = "2011-01-18 11:51:41";
echo date('d-m-Y H:i:s', strtotime($your_date));
demo

Timestamp to Date

Ho do I convert:
2010-12-24 11:39:43
to:
24/12 11:39
Thanks.
This should to the trick:
$newFormat = Date ( 'd/m H:i', StrToTime ( '2010-12-24 11:39:43' ) );
You use StrToTime to convert a string representation of a date to timestamp. You then feed that timestamp to the Date function that takes the format of the date as the first parameter.
Try:
$unixtime = strtotime("2010-12-24 11:39:43");
$newFormat = date("d/m H:i", $unixtime);
echo date("d/m H:i", strtotime("2010-12-24 11:39:43"));
$date = DateTime::createFromFormat('Y-m-d G:i:s', 2010-12-24 11:39:43); //You can simply tell DateTime accept your timestamp as is since PHP 5.3
echo $date->format('d/m G:i T'); //Will output what you wanted + Timezone Abbreviation (because of the T)

php convert date to MySQL date

I need to convert a date in this format:
November 28, 2009
to a MySQL date format:
2009-28-11
What's the best method to convert the date using PHP?
Improvised from: http://www.bigroom.co.uk/blog/dates-in-php-and-mysql
$mysqldate = date( 'Y-m-d', strtotime( $phpdate ) );
// Example:
$phpdate = 'November 20, 2009';
$mysqldate = date( 'Y-m-d', strtotime( $phpdate ) );
echo $mysqldate;
// output: 2009-11-20
If you want a "date" to be converted to "DateTime" this is the best way =
// for example: you have a string with the following
$dateFormat = 'd/m/Y';
$dateString = '02/12/2019';
// you can easily create DateTime using
$dateTime = \DateTime::createFromFormat($dateFormat, $dateString);
As described in the php documentation for createFromFormat.
And to answer completely on your question:
echo $dateTime->format('y-m-d');
I like to use strtotime and the date function as follows:
$mysql_date = date("Y-m-d", strtotime($source_date));
There are two options you can use which are strtotime or preg_split and sprintf. I recommend you use strtotime. The structure goes like this:
$date = 'November 28 2009';
$sqldate = date('Y-m-d', strtotime($date));
Make sure the Y is capital so it reads as 0000 otherwise it will read 00.

Categories