Click here to Skip to main content
15,917,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a date time picker which gives the result as 04/17/2012 00:00
I want to convert it to timestamp and i am trying this method, but it is not working.

PHP
$sqlDate = new DateTime();
$sqlDate->createFromFormat("HH:ii:ss dd/mm/YYYY",$mydate);


when i try

PHP
echo new datetime = ".$sqlDate;


it says Catchable fatal error: Object of class DateTime could not be converted to string.

Any help please?
Posted

hi,

try this
PHP
$a = strptime('22-09-2008', '%d-%m-%Y');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900)



hope it will help u

best Luck
 
Share this answer
 
import java.util.*;
import java.text.*;
import java.sql.Timestamp;
public class DateToTimestamp {
public static void main(String[] args) {
try { String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
java.sql.Timestamp timeStampDate = new
Timestamp(date.getTime());
System.out.println("Today is " +timeStampDate);
} catch (ParseException e)
{System.out.println("Exception :"+e); }

}
}


for PHP. USE this code


XML
function date_to_stamp( $date, $slash_time = true, $timezone = 'Europe/London', $expression = "#^\d{2}([^\d]*)\d{2}([^\d]*)\d{4}$#is" ) {
    $return = false;
    $_timezone = date_default_timezone_get();
    date_default_timezone_set( $timezone );
    if( preg_match( $expression, $date, $matches ) )
        $return = date( "Y-m-d " . ( $slash_time ? '00:00:00' : "h:i:s" ), strtotime( str_replace( array($matches[1], $matches[2]), '-', $date ) . ' ' . date("h:i:s") ) );
    date_default_timezone_set( $_timezone );
    return $return;
}

// expression may need changing in relation to timezone
echo date_to_stamp('19/03/1986', false) . '<br />';
echo date_to_stamp('19**03**1986', false) . '<br />';
echo date_to_stamp('19.03.1986') . '<br />';
echo date_to_stamp('19.03.1986', false, 'Asia/Aden') . '<br />';
echo date('Y-m-d h:i:s') . '<br />';

//1986-03-19 02:37:30
//1986-03-19 02:37:30
//1986-03-19 00:00:00
//1986-03-19 05:37:30
//2012-02-12 02:37:30
 
Share this answer
 
v2
Comments
adnama 17-Apr-12 9:18am    
what about the time?
fjdiewornncalwe 4-Apr-13 15:19pm    
Plagiarized from source.
Until you figure out that you need to provide a link when you copy/paste solutions from elsewhere, your answers will continue to get downvoted and flagged.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900